python3.4用循環(huán)往mysql5.7中寫數(shù)據(jù)并輸出的實現(xiàn)方法
更新時間:2017年06月20日 07:56:29 投稿:jingxian
下面小編就為大家?guī)硪黄猵ython3.4用循環(huán)往mysql5.7中寫數(shù)據(jù)并輸出的實現(xiàn)方法。小編覺得挺不錯的,現(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() # 關(guān)閉游標
conn.close() # 釋放數(shù)據(jù)庫資源
except Exception:
print("發(fā)生異常")
上面代碼是對的,但是是曲折的。
下面整理一下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = "blzhu"
"""
python study
Date:2017
"""
import pymysql
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ù)庫
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() # 關(guān)閉游標
conn.close() # 釋放數(shù)據(jù)庫資源
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語句
cursor.execute(sql)
# 提交到數(shù)據(jù)庫執(zhí)行
db.commit()
except:
# 如果發(fā)生錯誤則回滾
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()
#查詢
cursor.execute("select * from user")
results=cursor.fetchall()
for row in results:
name=row[0]
age=row[1]
#print(type(row[1])) #打印變量類型 <class 'str'>
print ("name=%s,age=%s" % \
(age, name))
以上這篇python3.4用循環(huán)往mysql5.7中寫數(shù)據(jù)并輸出的實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
獨立進程使用django模型及django.setup()使用
這篇文章主要介紹了獨立進程使用django模型(django.setup()使用),它提供了一種簡單且高效的方式來利用Django強大的功能,并使你的代碼更易于維護和擴展,需要的朋友可以參考下2023-07-07
使用pyinstaller打包PyQt4程序遇到的問題及解決方法
今天小編就為大家分享一篇使用pyinstaller打包PyQt4程序遇到的問題及解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
手把手教你jupyter?notebook更換環(huán)境的方法
在日常使用jupyter-notebook時,可能會碰到需要切換不同虛擬環(huán)境的場景,下面這篇文章主要給大家介紹了關(guān)于jupyter?notebook更換環(huán)境的方法,需要的朋友可以參考下2023-05-05

