第二十天
Python與MySQL交互(一)
1综慎、數(shù)據(jù)庫數(shù)據(jù)準(zhǔn)備
創(chuàng)建數(shù)據(jù)庫并添加數(shù)據(jù)
#學(xué)生表
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`province` varchar(20) NOT NULL,
`city` varchar(20) NOT NULL,
`town` varchar(20) NOT NULL,
`address` varchar(100) NOT NULL,
`num` char(10) DEFAULT NULL,
`cid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
)
#班級表
CREATE TABLE `class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
)
2示惊、Python中操作MySQL數(shù)據(jù)庫
2.1愉镰、引入模塊pymysql
pymysql類似java中的數(shù)據(jù)庫驅(qū)動,有了這個驅(qū)動我們才可以連接MySQL數(shù)據(jù)庫录择。pymysql是在 Python3.x 版本中用于連接 MySQL 服務(wù)器的一個庫隘竭,Python2中則使用mysqldb讼渊。我們要想引入pymysql,第一步就需要安裝pymysql菱皆。兩種方式:
1、pip3 install PyMySQL
2京痢、PyCharm安裝
2.2拯田、創(chuàng)建連接
import pymysql
#創(chuàng)建連接
db = pymysql.connect(host='localhost',port=3306,user='root',
passwd='root',database='python_db')
#對數(shù)據(jù)的增刪改查操作在這里
# 關(guān)閉連接
db.close()
其中各個參數(shù):
1、host吭产、連接的MySQL主機鸭轮,若是本地則為localhost窃爷;
2、port医吊、連接的MySQL主機端口逮京,默認(rèn)3306;
3草描、user策严、連接的用戶名;
4逛绵、passwd倔韭、連接的密碼狐肢;
5、database碟联、數(shù)據(jù)庫名字;
6鲤孵、charset、編碼方式贵试,建議utf-8
2.3凯正、獲取cursor
# 使用 cursor() 方法創(chuàng)建一個游標(biāo)對象 cursor
cursor = db.cursor()
2.4廊散、執(zhí)行操作
2.4.1、執(zhí)行查詢操作
2.4.1.1运准、查詢一行數(shù)據(jù)
import pymysql
#創(chuàng)建連接
db = pymysql.connect(host='localhost',port=3306,user='root',
passwd='root',database='python_db')
# 使用 cursor() 方法創(chuàng)建一個游標(biāo)對象 cursor
cursor = db.cursor()
#創(chuàng)建SQL語句 cursor.execute執(zhí)行
sql = 'select * from student where id = 1'
count = cursor.execute(sql) # count 查詢的同時 返回行數(shù)
print(count)
data = cursor.fetchone()
print(data)
# 關(guān)閉游標(biāo)與數(shù)據(jù)庫連接
cursor.close()
db.close()
2.4.1.1胁澳、查詢多行數(shù)據(jù)
import pymysql
#創(chuàng)建連接
db = pymysql.connect(host='localhost',port=3306,user='root',
passwd='root',database='python_db')
# 使用 cursor() 方法創(chuàng)建一個游標(biāo)對象 cursor
cursor = db.cursor()
#創(chuàng)建SQL語句 cursor.execute執(zhí)行
sql = 'select * from student'
count = cursor.execute(sql) # count 查詢的同時 返回行數(shù)
print(count)
data = cursor.fetchall()
if data:
for row in data:
id = row[0]
name = row[1]
province = row[2]
city = row[3]
town = row[4]
address = row[5]
num = row[6]
cid = row[7]
print('編號:%s,姓名:%s,學(xué)號:%s,地址:%s%s%s%s' %(id,name,num,province,city,town,address))
# 關(guān)閉游標(biāo)與數(shù)據(jù)庫連接
cursor.close()
db.close()
2.4.2韭畸、執(zhí)行添加操作
#添加
import pymysql
#創(chuàng)建連接
db = pymysql.connect(host='localhost',port=3306,user='root',
passwd='root',database='python_db')
# 使用 cursor() 方法創(chuàng)建一個游標(biāo)對象 cursor
cursor = db.cursor()
#創(chuàng)建SQL語句 cursor.execute執(zhí)行
sql = 'insert into Student(name,num,province,city,town,address,cid)' \
' values ("王五","5410050505","河南省","鄭州市","金水區(qū)","A地",1)'
try:
#執(zhí)行SQL語句
cursor.execute(sql)
# 提交執(zhí)行
db.commit()
except:
#發(fā)生異陈脚蹋回滾
db.rollback()
# 關(guān)閉游標(biāo)與數(shù)據(jù)庫連接
cursor.close()
db.close()
2.4.3败明、執(zhí)行修改操作
#修改
import pymysql
#創(chuàng)建連接
db = pymysql.connect(host='localhost',port=3306,user='root',
passwd='root',database='python_db')
# 使用 cursor() 方法創(chuàng)建一個游標(biāo)對象 cursor
cursor = db.cursor()
#創(chuàng)建SQL語句 cursor.execute執(zhí)行
sql = 'update Student set name = "%s" where id = %d' %('哈哈哈',3)
try:
#執(zhí)行SQL語句
cursor.execute(sql)
# 提交執(zhí)行
db.commit()
except:
#發(fā)生異程溃回滾
db.rollback()
# 關(guān)閉游標(biāo)與數(shù)據(jù)庫連接
cursor.close()
db.close()
2.4.4、執(zhí)行刪除操作
#刪除
import pymysql
#創(chuàng)建連接
db = pymysql.connect(host='localhost',port=3306,user='root',
passwd='root',database='python_db')
# 使用 cursor() 方法創(chuàng)建一個游標(biāo)對象 cursor
cursor = db.cursor()
#創(chuàng)建SQL語句 cursor.execute執(zhí)行
sql = 'delete from Student where id = %d' %(3)
try:
#執(zhí)行SQL語句
cursor.execute(sql)
# 提交執(zhí)行
db.commit()
except:
#發(fā)生異郴渲觯回滾
db.rollback()
# 關(guān)閉游標(biāo)與數(shù)據(jù)庫連接
cursor.close()
db.close()
2.5沥潭、關(guān)閉cursor與連接
及時資源的釋放嬉挡。
# 關(guān)閉游標(biāo)與數(shù)據(jù)庫連接
cursor.close()
db.close()
3汇恤、最后
Python與MySQL數(shù)據(jù)庫交互是比較簡單的因谎,添加/刪除/修改其實是一樣的颜懊,重在實踐。下次我們來說下多表的操作(查詢)匠璧。