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í)現(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()使用),它提供了一種簡(jiǎn)單且高效的方式來(lái)利用Django強(qiáng)大的功能,并使你的代碼更易于維護(hù)和擴(kuò)展,需要的朋友可以參考下2023-07-07
使用pyinstaller打包PyQt4程序遇到的問(wèn)題及解決方法
今天小編就為大家分享一篇使用pyinstaller打包PyQt4程序遇到的問(wèn)題及解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Python實(shí)現(xiàn)登陸文件驗(yàn)證方法
本篇文章中我們給大家分享了關(guān)于Python實(shí)現(xiàn)登陸文件驗(yàn)證的方法和技巧,有興趣的朋友們參考學(xué)習(xí)下。2018-10-10
手把手教你jupyter?notebook更換環(huán)境的方法
在日常使用jupyter-notebook時(shí),可能會(huì)碰到需要切換不同虛擬環(huán)境的場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于jupyter?notebook更換環(huán)境的方法,需要的朋友可以參考下2023-05-05

