Python 解析pymysql模塊操作數(shù)據(jù)庫(kù)的方法
pymysql 是 python 用來(lái)操作MySQL的第三方庫(kù),下面具體介紹和使用該庫(kù)的基本方法。
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()方法查詢后,通過(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é)果:
總的來(lái)說(shuō)和java進(jìn)行對(duì)比,在數(shù)據(jù)庫(kù)的連接 和對(duì)
數(shù)據(jù)集進(jìn)行的處理上,python體現(xiàn)的非常簡(jiǎn)潔,最主要易于使用和理解。人生苦短,我用python!
總結(jié)
以上所述是小編給大家介紹的Python 解析pymysql模塊操作數(shù)據(jù)庫(kù)的方法,希望對(duì)大家有所幫助!
- Python中操作mysql的pymysql模塊詳解
- Python中模塊pymysql查詢結(jié)果后如何獲取字段列表
- Python中pymysql 模塊的使用詳解
- 使用python連接mysql數(shù)據(jù)庫(kù)之pymysql模塊的使用
- Python 中使用 PyMySQL模塊操作數(shù)據(jù)庫(kù)的方法
- python之pymysql模塊簡(jiǎn)單應(yīng)用示例代碼
- Python使用pymysql模塊操作mysql增刪改查實(shí)例分析
- Python pymysql模塊安裝并操作過(guò)程解析
- python使用pymysql模塊操作MySQL
- Python中使用PyMySQL模塊的方法詳解
相關(guān)文章
詳解Selenium如何實(shí)現(xiàn)獲取cookies并保存
這篇文章主要為大家詳細(xì)介紹了Selenium如何實(shí)現(xiàn)獲取cookies保存起來(lái)用于下次訪問(wèn),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-05-05matplotlib之pyplot模塊坐標(biāo)軸標(biāo)簽設(shè)置使用(xlabel()、ylabel())
這篇文章主要介紹了matplotlib之pyplot模塊坐標(biāo)軸標(biāo)簽設(shè)置使用(xlabel()、ylabel()),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02如何解決安裝包過(guò)程中的Requirement already satisfied:問(wèn)題
這篇文章主要介紹了如何解決安裝包過(guò)程中的Requirement already satisfied:問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Python圖像閾值化處理及算法比對(duì)實(shí)例解析
這篇文章主要介紹了Python圖像閾值化處理及算法比對(duì)實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06python實(shí)現(xiàn)emoji對(duì)齊特殊字符對(duì)齊高級(jí)文本對(duì)齊
這篇文章主要為大家介紹了python實(shí)現(xiàn)emoji對(duì)齊特殊字符對(duì)齊高級(jí)文本對(duì)齊方法實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Python實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的示例代碼
Tkinter作為 Python GUI 開(kāi)發(fā)工具之一,它具有 GUI 軟件包的必備的常用功能。本文就將利用Tkinter編寫簡(jiǎn)易的計(jì)算器,感興趣的可以了解一下2022-11-11