日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

python3.4用循环往mysql5.7中写数据并输出

發(fā)布時間:2025/7/14 数据库 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3.4用循环往mysql5.7中写数据并输出 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "blzhu" """ python study Date:2017 """ import pymysql # import MySQLdb #python2中的產(chǎn)物try:# 獲取一個數(shù)據(jù)庫連接,注意如果是UTF-8類型的,需要制定數(shù)據(jù)庫conn = pymysql.connect(host='localhost', user='root', passwd='root', db='zbltest1', port=3306, charset='utf8')cur = conn.cursor() # 獲取一個游標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ù)寫入數(shù)據(jù)庫# 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')")#錯誤#cur.execute("INSERT student ('id','name','gender') VALUES ('4', 'zbl4', 'man')") cur.execute('select * from student')# data=cur.fetchall()for d in cur:# 注意int類型需要使用str函數(shù)轉(zhuǎn)義print("ID: " + str(d[0]) + ' 名字: ' + d[1] + " 性別: " + d[2])print("row_number:", (cur.rownumber))# print('hello') cur.close() # 關閉游標conn.close() # 釋放數(shù)據(jù)庫資源 except Exception:print("發(fā)生異常")

上面代碼是對的,但是是曲折的。

下面整理一下:

1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # __author__ = "blzhu" 4 """ 5 python study 6 Date:2017 7 """ 8 import pymysql 9 try: 10 # 獲取一個數(shù)據(jù)庫連接,注意如果是UTF-8類型的,需要制定數(shù)據(jù)庫 11 conn = pymysql.connect(host='localhost', user='root', passwd='root', db='zbltest1', port=3306, charset='utf8') 12 cur = conn.cursor() # 獲取一個游標 13 for i in range(1, 10): 14 zbl_id = str(i) 15 zbl_name = 'zbl'+str(i) 16 zbl_gender = 'man' 17 # print("%s,%s,%s" % (zbl_id,zbl_name,zbl_gender)) 18 # sql = "insert student VALUES (id='%s',name='%s',gender='%s')" % (zbl_id,zbl_name,zbl_gender) 19 sql = "insert student VALUES ('%s','%s','%s')" % (zbl_id, zbl_name, zbl_gender) 20 # print(sql) 21 cur.execute(sql) 22 conn.commit()# 將數(shù)據(jù)寫入數(shù)據(jù)庫 23 cur.execute('select * from student') 24 # data=cur.fetchall() 25 for d in cur: 26 # 注意int類型需要使用str函數(shù)轉(zhuǎn)義 27 print("ID: " + str(d[0]) + ' 名字: ' + d[1] + " 性別: " + d[2]) 28 print("row_number:", (cur.rownumber)) 29 # print('hello') 30 31 cur.close() # 關閉游標 32 conn.close() # 釋放數(shù)據(jù)庫資源 33 except Exception: 34 print("發(fā)生異常")

學習的幾個地方:

http://blog.csdn.net/nuli888/article/details/51960571

1 #!/usr/bin/python3 2 import pymysql 3 import types 4 5 db=pymysql.connect("localhost","root","123456","python"); 6 7 cursor=db.cursor() 8 9 #創(chuàng)建user表 10 cursor.execute("drop table if exists user") 11 sql="""CREATE TABLE IF NOT EXISTS `user` ( 12 `id` int(11) NOT NULL AUTO_INCREMENT, 13 `name` varchar(255) NOT NULL, 14 `age` int(11) NOT NULL, 15 PRIMARY KEY (`id`) 16 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0""" 17 18 cursor.execute(sql) 19 20 21 #user插入數(shù)據(jù) 22 sql="""INSERT INTO `user` (`name`, `age`) VALUES 23 ('test1', 1), 24 ('test2', 2), 25 ('test3', 3), 26 ('test4', 4), 27 ('test5', 5), 28 ('test6', 6);""" 29 30 try: 31 # 執(zhí)行sql語句 32 cursor.execute(sql) 33 # 提交到數(shù)據(jù)庫執(zhí)行 34 db.commit() 35 except: 36 # 如果發(fā)生錯誤則回滾 37 db.rollback() 38 39 40 #更新 41 id=1 42 sql="update user set age=100 where id='%s'" % (id) 43 try: 44 cursor.execute(sql) 45 db.commit() 46 except: 47 db.rollback() 48 49 #刪除 50 id=2 51 sql="delete from user where id='%s'" % (id) 52 try: 53 cursor.execute(sql) 54 db.commit() 55 except: 56 db.rollback() 57 58 59 #查詢 60 cursor.execute("select * from user") 61 62 results=cursor.fetchall() 63 64 for row in results: 65 name=row[0] 66 age=row[1] 67 #print(type(row[1])) #打印變量類型 <class 'str'> 68 69 print ("name=%s,age=%s" % \ 70 (age, name)) View Code

http://www.runoob.com/python/python-mysql.html

?

http://www.cnblogs.com/lei0213/p/6002921.html

?

http://blog.csdn.net/magicbreaker/article/details/41811519

?

http://blog.csdn.net/bwlab/article/details/51146640

轉(zhuǎn)載于:https://www.cnblogs.com/zhubinglong/p/7051228.html

總結(jié)

以上是生活随笔為你收集整理的python3.4用循环往mysql5.7中写数据并输出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。