最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python3.4用循環(huán)往mysql5.7中寫(xiě)數(shù)據(jù)并輸出的實(shí)現(xiàn)方法

 更新時(shí)間:2017年06月20日 07:56:29   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇python3.4用循環(huán)往mysql5.7中寫(xiě)數(shù)據(jù)并輸出的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

如下所示:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "blzhu"
"""
python study
Date:2017
"""
import pymysql
# import MySQLdb #python2中的產(chǎn)物

try:
  # 獲取一個(gè)數(shù)據(jù)庫(kù)連接,注意如果是UTF-8類(lèi)型的,需要制定數(shù)據(jù)庫(kù)
  conn = pymysql.connect(host='localhost', user='root', passwd='root', db='zbltest1', port=3306, charset='utf8')
  cur = conn.cursor() # 獲取一個(gè)游標(biāo)
  for i in range(1, 10):
    zbl_id = str(i)
    zbl_name = 'zbl'+str(i)
    zbl_gender = 'man'
    # print("%s,%s,%s" % (zbl_id,zbl_name,zbl_gender))
    # sql = "insert student VALUES (id='%s',name='%s',gender='%s')" % (zbl_id,zbl_name,zbl_gender)
    sql = "insert student VALUES ('%s','%s','%s')" % (zbl_id, zbl_name, zbl_gender)
    # print(sql)
    cur.execute(sql)
  conn.commit()# 將數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù)

    # try:
    # cur.execute(sql)
      # cur.commit()
    # except:
    #   cur.rollback()
    #cur.execute("""INSERT INTO 'student' ('id','name','gender') VALUES (%s,%s,%s ,(zbl_id,zbl_name,zbl_gender,))""")
    #cur.execute("""INSERT INTO 'student' ('id','name','gender') VALUES (zbl_id,zbl_name,zbl_gender)""")

    # cur.execute("INSERT student VALUES (zbl_id,zbl_name,zbl_gender)")

  # cur.execute("INSERT student VALUES ('4', 'zbl4', 'man')")# 正確
  #cur.execute("INSERT INTO 'student' ('id','name','gender') VALUES ('4', 'zbl4', 'man')")#錯(cuò)誤
  #cur.execute("INSERT student ('id','name','gender') VALUES ('4', 'zbl4', 'man')")


  cur.execute('select * from student')
  # data=cur.fetchall()
  for d in cur:
    # 注意int類(lèi)型需要使用str函數(shù)轉(zhuǎn)義
    print("ID: " + str(d[0]) + ' 名字: ' + d[1] + " 性別: " + d[2])
  print("row_number:", (cur.rownumber))
  # print('hello')

  cur.close() # 關(guān)閉游標(biāo)
  conn.close() # 釋放數(shù)據(jù)庫(kù)資源
except Exception:
  print("發(fā)生異常")

上面代碼是對(duì)的,但是是曲折的。

下面整理一下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "blzhu"
"""
python study
Date:2017
"""
import pymysql
try:
  # 獲取一個(gè)數(shù)據(jù)庫(kù)連接,注意如果是UTF-8類(lèi)型的,需要制定數(shù)據(jù)庫(kù)
  conn = pymysql.connect(host='localhost', user='root', passwd='root', db='zbltest1', port=3306, charset='utf8')
  cur = conn.cursor() # 獲取一個(gè)游標(biāo)
  for i in range(1, 10):
    zbl_id = str(i)
    zbl_name = 'zbl'+str(i)
    zbl_gender = 'man'
    # print("%s,%s,%s" % (zbl_id,zbl_name,zbl_gender))
    # sql = "insert student VALUES (id='%s',name='%s',gender='%s')" % (zbl_id,zbl_name,zbl_gender)
    sql = "insert student VALUES ('%s','%s','%s')" % (zbl_id, zbl_name, zbl_gender)
    # print(sql)
    cur.execute(sql)
  conn.commit()# 將數(shù)據(jù)寫(xiě)入數(shù)據(jù)庫(kù)
  cur.execute('select * from student')
  # data=cur.fetchall()
  for d in cur:
    # 注意int類(lèi)型需要使用str函數(shù)轉(zhuǎn)義
    print("ID: " + str(d[0]) + ' 名字: ' + d[1] + " 性別: " + d[2])
  print("row_number:", (cur.rownumber))
  # print('hello')

  cur.close() # 關(guān)閉游標(biāo)
  conn.close() # 釋放數(shù)據(jù)庫(kù)資源
except Exception:
  print("發(fā)生異常")
#!/usr/bin/python3
import pymysql
import types

db=pymysql.connect("localhost","root","123456","python");

cursor=db.cursor()

#創(chuàng)建user表
cursor.execute("drop table if exists user")
sql="""CREATE TABLE IF NOT EXISTS `user` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `name` varchar(255) NOT NULL,
   `age` int(11) NOT NULL,
   PRIMARY KEY (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0"""

cursor.execute(sql)


#user插入數(shù)據(jù)
sql="""INSERT INTO `user` (`name`, `age`) VALUES
('test1', 1),
('test2', 2),
('test3', 3),
('test4', 4),
('test5', 5),
('test6', 6);"""

try:
  # 執(zhí)行sql語(yǔ)句
  cursor.execute(sql)
  # 提交到數(shù)據(jù)庫(kù)執(zhí)行
  db.commit()
except:
  # 如果發(fā)生錯(cuò)誤則回滾
  db.rollback()
  
  
#更新
id=1
sql="update user set age=100 where id='%s'" % (id)
try:
  cursor.execute(sql)
  db.commit()
except:
  db.rollback()
  
#刪除
id=2
sql="delete from user where id='%s'" % (id)
try:
  cursor.execute(sql)
  db.commit()
except:
  db.rollback()
  
  
#查詢(xún)
cursor.execute("select * from user")

results=cursor.fetchall()

for row in results:
  name=row[0]
  age=row[1]
  #print(type(row[1])) #打印變量類(lèi)型 <class 'str'>

  print ("name=%s,age=%s" % \
       (age, name))

以上這篇python3.4用循環(huán)往mysql5.7中寫(xiě)數(shù)據(jù)并輸出的實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python 讀取串口數(shù)據(jù)的示例

    python 讀取串口數(shù)據(jù)的示例

    這篇文章主要介紹了python 讀取串口數(shù)據(jù)的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-11-11
  • Python實(shí)現(xiàn)的計(jì)數(shù)排序算法示例

    Python實(shí)現(xiàn)的計(jì)數(shù)排序算法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的計(jì)數(shù)排序算法,簡(jiǎn)單描述了計(jì)數(shù)排序的算法原理并結(jié)合具體實(shí)例形式分析了Python計(jì)數(shù)排序的相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下
    2017-11-11
  • 獨(dú)立進(jìn)程使用django模型及django.setup()使用

    獨(dú)立進(jìn)程使用django模型及django.setup()使用

    這篇文章主要介紹了獨(dú)立進(jìn)程使用django模型(django.setup()使用),它提供了一種簡(jiǎn)單且高效的方式來(lái)利用Django強(qiáng)大的功能,并使你的代碼更易于維護(hù)和擴(kuò)展,需要的朋友可以參考下
    2023-07-07
  • python實(shí)現(xiàn)布爾型盲注的示例代碼

    python實(shí)現(xiàn)布爾型盲注的示例代碼

    這篇文章主要介紹了python實(shí)現(xiàn)sql布爾盲注的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 使用pyinstaller打包PyQt4程序遇到的問(wèn)題及解決方法

    使用pyinstaller打包PyQt4程序遇到的問(wèn)題及解決方法

    今天小編就為大家分享一篇使用pyinstaller打包PyQt4程序遇到的問(wèn)題及解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • Python實(shí)現(xiàn)登陸文件驗(yàn)證方法

    Python實(shí)現(xiàn)登陸文件驗(yàn)證方法

    本篇文章中我們給大家分享了關(guān)于Python實(shí)現(xiàn)登陸文件驗(yàn)證的方法和技巧,有興趣的朋友們參考學(xué)習(xí)下。
    2018-10-10
  • python轉(zhuǎn)換字符串為摩爾斯電碼的方法

    python轉(zhuǎn)換字符串為摩爾斯電碼的方法

    這篇文章主要介紹了python轉(zhuǎn)換字符串為摩爾斯電碼的方法,涉及Python字符串及編碼操作的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-07-07
  • 手把手教你jupyter?notebook更換環(huán)境的方法

    手把手教你jupyter?notebook更換環(huán)境的方法

    在日常使用jupyter-notebook時(shí),可能會(huì)碰到需要切換不同虛擬環(huán)境的場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于jupyter?notebook更換環(huán)境的方法,需要的朋友可以參考下
    2023-05-05
  • Python中的json內(nèi)置庫(kù)詳解

    Python中的json內(nèi)置庫(kù)詳解

    這篇文章主要介紹了Python中的json內(nèi)置庫(kù)詳解,在學(xué)習(xí)做自動(dòng)化測(cè)試的過(guò)程中,python 里有一個(gè)內(nèi)置的 json 庫(kù),必須要學(xué)習(xí)好,json 是用于存儲(chǔ)和交換數(shù)據(jù)的語(yǔ)法,是一種輕量級(jí)的數(shù)據(jù)交換式使用場(chǎng)景,需要的朋友可以參考下
    2023-08-08
  • 講解Python?中的?with?關(guān)鍵字

    講解Python?中的?with?關(guān)鍵字

    這篇文章主要介紹了講解Python?中的with關(guān)鍵字,文章基于python的相關(guān)資料展開(kāi)?with?語(yǔ)句的一些基本概念和用法及其底層工作原理,下文更多內(nèi)容感興趣的小伙伴可以參考一下
    2022-05-05

最新評(píng)論

大宁县| 吴堡县| 江源县| 沁源县| 河西区| 宁南县| 九龙坡区| 盖州市| 河池市| 玛纳斯县| 柯坪县| 军事| 华安县| 商城县| 大方县| 健康| 赤城县| 大丰市| 沂南县| 从化市| 夹江县| 团风县| 安陆市| 萍乡市| 沙洋县| 齐齐哈尔市| 合水县| 贵南县| 天台县| 庆元县| 高清| 盐边县| 六安市| 芷江| 射阳县| 乌什县| 武汉市| 津南区| 安顺市| 西丰县| 迭部县|