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ù)變化,會馬上反應到同一連接中的其它游標對象。但是不同數(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é)果集當中返回一條記錄的序列,無則返回None
fetchmany([size=cursor.arraysize])
從結(jié)果集當中返回小于或等于size的記錄序列,無則返回空序列,size默認是整個游標的行數(shù)
fetchall()
從結(jié)果集當中返回所有的行數(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)文章
Python日期格式和字符串格式相互轉(zhuǎn)換的方法
這篇文章主要介紹了Python日期格式和字符串格式相互轉(zhuǎn)換的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
python實現(xiàn)Pyecharts實現(xiàn)動態(tài)地圖(Map、Geo)
這篇文章主要為大家詳細介紹了python實現(xiàn)Pyecharts實現(xiàn)動態(tài)地圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03
python調(diào)用百度REST API實現(xiàn)語音識別
這篇文章主要為大家詳細介紹了python調(diào)用百度REST API實現(xiàn)語音識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Python實現(xiàn)的用戶登錄系統(tǒng)功能示例
這篇文章主要介紹了Python實現(xiàn)的用戶登錄系統(tǒng)功能,涉及Python流程控制及字符串判斷等相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
Python2和Python3之間的str處理方式導致亂碼的講解
今天小編就為大家分享一篇關(guān)于Python2和Python3之間的str處理方式導致亂碼的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
Python configparser模塊封裝及構(gòu)造配置文件
這篇文章主要介紹了Python configparser模塊封裝及構(gòu)造配置文件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08

