Python 解析pymysql模塊操作數(shù)據(jù)庫的方法
pymysql 是 python 用來操作MySQL的第三方庫,下面具體介紹和使用該庫的基本方法。
1.建立數(shù)據(jù)庫連接
通過 connect 函數(shù)中 parameter 參數(shù) 建立連接,連接成功返回Connection對象
import pymysql #建立數(shù)據(jù)庫連接 connection = pymysql.connect(host = 'localhost', user = 'root', password = '123456', database = 'mydb', charset = 'utf8' ) #print(connection)
pymysql.connect()函數(shù)中常用的連接參數(shù)有以下幾種:
- host:數(shù)據(jù)庫主機名或者ip地址
- port:端口號
- user:數(shù)據(jù)庫的賬號
- password 或 passwd:數(shù)據(jù)庫的密碼
- database 或 db:數(shù)據(jù)庫的名字
- charset:編碼方式
Connection對象的重要方法:
- close() 關(guān)閉數(shù)據(jù)庫連接
- commit() 提交數(shù)據(jù)庫事物
- rollback() 回滾數(shù)據(jù)庫事務(wù)
- cursor() 獲得 Cursor游標對象
2.創(chuàng)建游標
一個Cursor游標對象,暫時保存了SQL操作所影響到的數(shù)據(jù),相同的數(shù)據(jù)庫連接創(chuàng)建的游標所引起的數(shù)據(jù)變化,會馬上反應(yīng)到同一連接中的其它游標對象。但是不同數(shù)據(jù)庫連接中的游標對象,是否能及時反映出來,則與數(shù)據(jù)庫事物管理有關(guān)。
Cursor對象基本方法和屬性:
execute(operation,[parameters])
執(zhí)行一條SQL語句,operation時SQL語句,parameters是其參數(shù)。返回值是整數(shù),表示執(zhí)行SQL語句影響的行數(shù)
executemany(operation,[parameters])
批量執(zhí)行SQL語句
callproc(procname,[parameters])
執(zhí)行存儲過程,procname是存儲過程名
使用execute()和executemany()方法查詢后,通過以下提取方法提取結(jié)果集
fetchone()
從結(jié)果集當(dāng)中返回一條記錄的序列,無則返回None
fetchmany([size=cursor.arraysize])
從結(jié)果集當(dāng)中返回小于或等于size的記錄序列,無則返回空序列,size默認是整個游標的行數(shù)
fetchall()
從結(jié)果集當(dāng)中返回所有的行數(shù)
3.建立數(shù)據(jù)庫(這里我使用的是NaviCat)
創(chuàng)建一個名為pydb的數(shù)據(jù)庫,表名為user,字段name和userid
數(shù)據(jù)的查找
#建立數(shù)據(jù)庫連接 connection = pymysql.connect(host = 'localhost', user = 'root', password = '123456', database = 'mydb', charset = 'utf8' ) #print(connection) try: #創(chuàng)建游標對象 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])) #游標自動關(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ù)庫事務(wù) connection.commit() except pymysql.DatabaseError: #數(shù)據(jù)庫事務(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é)果:
總的來說和java進行對比,在數(shù)據(jù)庫的連接 和對
數(shù)據(jù)集進行的處理上,python體現(xiàn)的非常簡潔,最主要易于使用和理解。人生苦短,我用python!
總結(jié)
以上所述是小編給大家介紹的Python 解析pymysql模塊操作數(shù)據(jù)庫的方法,希望對大家有所幫助!
相關(guān)文章
詳解Selenium如何實現(xiàn)獲取cookies并保存
這篇文章主要為大家詳細介紹了Selenium如何實現(xiàn)獲取cookies保存起來用于下次訪問,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-05-05matplotlib之pyplot模塊坐標軸標簽設(shè)置使用(xlabel()、ylabel())
這篇文章主要介紹了matplotlib之pyplot模塊坐標軸標簽設(shè)置使用(xlabel()、ylabel()),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02如何解決安裝包過程中的Requirement already satisfied:問題
這篇文章主要介紹了如何解決安裝包過程中的Requirement already satisfied:問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11python實現(xiàn)emoji對齊特殊字符對齊高級文本對齊
這篇文章主要為大家介紹了python實現(xiàn)emoji對齊特殊字符對齊高級文本對齊方法實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11