python与数据库交互的模块pymysql
?
一、Mysql
?
1、前提
pip install pymysql
?
import pymysql
?
2、詳情
?
Connection對(duì)象 =====>用于連接數(shù)據(jù)庫(kù)
?
用于建立與數(shù)據(jù)庫(kù)的連接
創(chuàng)建對(duì)象:調(diào)用connect()方法
conn=connect(參數(shù)列表)
參數(shù)host:連接的mysql主機(jī),如果本機(jī)是'localhost'
參數(shù)port:連接的mysql主機(jī)的端口,默認(rèn)是3306
參數(shù)db:數(shù)據(jù)庫(kù)的名稱
參數(shù)user:連接的用戶名
參數(shù)password:連接的密碼
參數(shù)charset:通信采用的編碼方式,默認(rèn)是'gb2312',要求與數(shù)據(jù)庫(kù)創(chuàng)建時(shí)指定的編碼一致,否則中文會(huì)亂碼
?
?
對(duì)象的方法
?
close()關(guān)閉連接
commit()事務(wù),所以需要提交才會(huì)生效 ??=========>注意:默認(rèn)開啟事務(wù)模式,所以執(zhí)行對(duì)數(shù)據(jù)變更的操作后要執(zhí)行一下commit()方法才能對(duì)物理數(shù)據(jù)實(shí)現(xiàn)修改
rollback()事務(wù),放棄之前的操作
cursor()返回Cursor對(duì)象,用于執(zhí)行sql語句并獲得結(jié)果
?
----------------------------------------------------------------------------------------------------------------------------------------------------------------
?
Cursor對(duì)象 =======>用于執(zhí)行sql語句
?
執(zhí)行sql語句
創(chuàng)建對(duì)象:調(diào)用Connection對(duì)象的cursor()方法
cursor1=conn.cursor()
?
?
對(duì)象的方法
?
close()關(guān)閉
?
execute(operation [1,2,])執(zhí)行語句,返回受影響的行數(shù)
executemany(sql, ((1,),(2,))) ?====>批量數(shù)據(jù)用這個(gè)只需要執(zhí)行一次sql就可以了,tmp為元組,里面元素也為元組,即元組套元組 ??====>遇到大量數(shù)據(jù)需重復(fù)調(diào)用execute(sql,[])時(shí),改用executemany只需調(diào)用一次即可,測(cè)試發(fā)現(xiàn)速度會(huì)快很多
例:
sql = "insert into person(name, age, telephone) values(%s, %s, %s)" ?
tmp = (('ninini', 89, '888999'), ('koko', 900, '999999'))
executemany(sql, tmp)
?
fetchone()執(zhí)行查詢語句時(shí),獲取查詢結(jié)果集的第一個(gè)行數(shù)據(jù),返回一個(gè)元組
?
next()執(zhí)行查詢語句時(shí),獲取當(dāng)前行的下一行
?
fetchall()執(zhí)行查詢時(shí),獲取結(jié)果集的所有行,一行構(gòu)成一個(gè)元組,再將這些元組裝入一個(gè)元組返回
?
scroll(value[,mode])將行指針移動(dòng)到某個(gè)位置
mode表示移動(dòng)的方式
mode的默認(rèn)值為relative,表示基于當(dāng)前行移動(dòng)到value,value為正則向下移動(dòng),value為負(fù)則向上移動(dòng)
mode的值為absolute,表示基于第一條數(shù)據(jù)的位置,第一條數(shù)據(jù)的位置為0
?
?
對(duì)象的屬性
?
rowcount只讀屬性,表示最近一次execute()執(zhí)行后受影響的行數(shù)
connection獲得當(dāng)前連接對(duì)象
?
----------------------------------------------------------------------------------------------------------------------------------------------------------------
?
增加
?
創(chuàng)建testInsert.py文件,向?qū)W生表中插入一條數(shù)據(jù)
#encoding=utf-8
import pymysql
try:
????conn=pymysql.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')
????cs1=conn.cursor()
????count=cs1.execute("insert into students(name) values('張良')") ??====>count表示受影響條數(shù)
????print count
????conn.commit()
????cs1.close()
????conn.close()
except Exception,e:
????print e.message
?
?
?
修改
?
創(chuàng)建testUpdate.py文件,修改學(xué)生表的一條數(shù)據(jù)
#encoding=utf-8
import pymysql
try:
????conn=pymysql.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')
????cs1=conn.cursor()
????count=cs1.execute("update students set sname='劉邦' where id=6") ???====>count表示受影響條數(shù)
????print count
????conn.commit()
????cs1.close()
????conn.close()
except Exception,e:
????print e.message
?
?
?
刪除
?
創(chuàng)建testDelete.py文件,刪除學(xué)生表的一條數(shù)據(jù)
#encoding=utf-8
import pymysql
try:
????conn=pymysql.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')
????cs1=conn.cursor()
????count=cs1.execute("delete from students where id=6") ??====>count表示受影響條數(shù)
????print count
????conn.commit()
????cs1.close()
????conn.close()
except Exception,e:
????print e.message
?
?
?
查詢
?
創(chuàng)建testSelect.py文件,查詢學(xué)生表中一條數(shù)據(jù)
#encoding=utf-8
import pymysql
try:
????conn=pymysql.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')
????cs1=conn.cursor()
????count=cs1.execute("select * from ?students where name=%s", ['張三']) ??====>count表示查詢到的條數(shù)(即:受影響的條數(shù))
????result = cs1.fetchall() ??=====>result為查詢到的具體數(shù)據(jù)(用元組表示)
????conn.commit()
????cs1.close()
????conn.close()
except Exception,e:
????print e.message
?
?
?
?
?
?
sql語句參數(shù)化(解決sql注入問題)
?
創(chuàng)建testInsertParam.py文件,向?qū)W生表中插入一條數(shù)據(jù)
#encoding=utf-8
import pymysql
try:
????conn=pymysql.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')
????cs1=conn.cursor()
????sname=input("請(qǐng)輸入學(xué)生姓名:")
????params=[sname]
????count=cs1.execute('insert into students(sname) values(%s)',params) ?====>此處的%s不是字符串的格式化,而是一個(gè)占位符,字符串格式化后跟的是'%',這里跟的是','!!! ?利用execute提供的這種方式無論用戶輸入什么都不怕了。
????print count
????conn.commit()
????cs1.close()
????conn.close()
except Exception,e:
????print e.message
?
?
?
其它語句
?
cursor對(duì)象的execute()方法,也可以用于執(zhí)行create table等語句
建議在開發(fā)之初,就創(chuàng)建好數(shù)據(jù)庫(kù)表結(jié)構(gòu),不要在這里執(zhí)行
?
?
?
?
?
?
?
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/baihualin/p/11288631.html
總結(jié)
以上是生活随笔為你收集整理的python与数据库交互的模块pymysql的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 超级航母战力超辽宁舰6倍
- 下一篇: Azure Redis Cache (3