您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在Python中使用pymysql模塊操作數(shù)據(jù)庫(kù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
1.建立數(shù)據(jù)庫(kù)連接
通過(guò) connect 函數(shù)中 parameter 參數(shù) 建立連接,連接成功返回Connection對(duì)象
import pymysql #建立數(shù)據(jù)庫(kù)連接 connection = pymysql.connect(host = 'localhost', user = 'root', password = '123456', database = 'mydb', charset = 'utf8' ) #print(connection)
pymysql.connect()函數(shù)中常用的連接參數(shù)有以下幾種:
host:數(shù)據(jù)庫(kù)主機(jī)名或者ip地址
port:端口號(hào)
user:數(shù)據(jù)庫(kù)的賬號(hào)
password 或 passwd:數(shù)據(jù)庫(kù)的密碼
database 或 db:數(shù)據(jù)庫(kù)的名字
charset:編碼方式
Connection對(duì)象的重要方法:
close() 關(guān)閉數(shù)據(jù)庫(kù)連接
commit() 提交數(shù)據(jù)庫(kù)事物
rollback() 回滾數(shù)據(jù)庫(kù)事務(wù)
cursor() 獲得 Cursor游標(biāo)對(duì)象
2.創(chuàng)建游標(biāo)
一個(gè)Cursor游標(biāo)對(duì)象,暫時(shí)保存了SQL操作所影響到的數(shù)據(jù),相同的數(shù)據(jù)庫(kù)連接創(chuàng)建的游標(biāo)所引起的數(shù)據(jù)變化,會(huì)馬上反應(yīng)到同一連接中的其它游標(biāo)對(duì)象。但是不同數(shù)據(jù)庫(kù)連接中的游標(biāo)對(duì)象,是否能及時(shí)反映出來(lái),則與數(shù)據(jù)庫(kù)事物管理有關(guān)。
Cursor對(duì)象基本方法和屬性:
execute(operation,[parameters])
執(zhí)行一條SQL語(yǔ)句,operation時(shí)SQL語(yǔ)句,parameters是其參數(shù)。返回值是整數(shù),表示執(zhí)行SQL語(yǔ)句影響的行數(shù)
executemany(operation,[parameters])
批量執(zhí)行SQL語(yǔ)句
callproc(procname,[parameters])
執(zhí)行存儲(chǔ)過(guò)程,procname是存儲(chǔ)過(guò)程名
使用execute()和executemany()方法查詢(xún)后,通過(guò)以下提取方法提取結(jié)果集
fetchone()
從結(jié)果集當(dāng)中返回一條記錄的序列,無(wú)則返回None
fetchmany([size=cursor.arraysize])
從結(jié)果集當(dāng)中返回小于或等于size的記錄序列,無(wú)則返回空序列,size默認(rèn)是整個(gè)游標(biāo)的行數(shù)
fetchall()
從結(jié)果集當(dāng)中返回所有的行數(shù)
3.建立數(shù)據(jù)庫(kù)(這里我使用的是NaviCat)
創(chuàng)建一個(gè)名為pydb的數(shù)據(jù)庫(kù),表名為user,字段name和userid
數(shù)據(jù)的查找
#建立數(shù)據(jù)庫(kù)連接 connection = pymysql.connect(host = 'localhost', user = 'root', password = '123456', database = 'mydb', charset = 'utf8' ) #print(connection) try: #創(chuàng)建游標(biāo)對(duì)象 with connection.cursor() as cursor: #執(zhí)行SQL操作 sql = 'select name, userid from user where userid >%(id)s' cursor.execute(sql, {'id':0}) #提取數(shù)據(jù)集 result_set = cursor.fetchall() for row in result_set: print('id:{0} - name:{1}'.format(row[1],row[0])) #游標(biāo)自動(dòng)關(guān)閉 finally: #關(guān)閉連接 connection.close()
數(shù)據(jù)插入
#數(shù)據(jù)增加 connection = pymysql.connect(host = 'localhost', user = 'root', password = '123456', database = 'mydb', charset = 'utf8' ) try: with connection.cursor() as cursor: sql = 'insert into user (userid,name) values (%s,%s)' cursor.execute(sql,(3,'cc')) #affectcount = cursor.execute(sql,(3,'cc')) #print('影響的數(shù)據(jù)行數(shù):{0}'.format(affectcount)) #提交數(shù)據(jù)庫(kù)事務(wù) connection.commit() except pymysql.DatabaseError: #數(shù)據(jù)庫(kù)事務(wù)回滾 connection.rollback() finally: connection.close()
執(zhí)行結(jié)果:
數(shù)據(jù)更新
#數(shù)據(jù)更新 connection = pymysql.connect(host = 'localhost', user = 'root', password = '123456', database = 'mydb', charset = 'utf8' ) #print(connection) try: with connection.cursor() as cursor: sql = 'update user set name = %s where userid > %s' cursor.execute(sql,('Tom',2)) #提交事務(wù) connection.commit() print('更新成功') except pymysql.DatabaseError as e: connection.rollback() print(e) finally: connection.close()
執(zhí)行結(jié)果:
數(shù)據(jù)刪除
#數(shù)據(jù)刪除 connection = pymysql.connect(host = 'localhost', user = 'root', password = '123456', database = 'mydb', charset = 'utf8' ) try: with connection.cursor() as cursor: sql = 'delete from user where userid = %s' cursor.execute(sql,(1)) #提交事務(wù) connection.commit() print("刪除成功") except pymysql.DatabaseError as e: connection.rollback() print(e) finally: connection.close()
執(zhí)行結(jié)果:
關(guān)于怎么在Python中使用pymysql模塊操作數(shù)據(jù)庫(kù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。