Python數(shù)據(jù)庫(kù)編程之pymysql詳解
Python數(shù)據(jù)庫(kù)編程之pymysql
學(xué)習(xí)之前務(wù)必安裝MySQL并已啟動(dòng)相關(guān)服務(wù)。
一、pymsql的安裝
在python3的環(huán)境中直接使用以下命令即可:
pip install pymysql #或者 pip3 install pymysql
安裝完畢后可使用以下命令查看:
pip list | grep PyMySQL #注意大小寫(xiě)
結(jié)果如下:

二、連接數(shù)據(jù)庫(kù)
pymysql連接數(shù)據(jù)庫(kù)使用的是 pymsql.connect() 函數(shù),其常用參數(shù)如下:
| 參數(shù) | 說(shuō)明 |
|---|---|
| dsn | 數(shù)據(jù)源名稱(chēng),給出該參數(shù)表示數(shù)據(jù)庫(kù)依賴(lài) |
| host=None | 數(shù)據(jù)庫(kù)連接地址 |
| user=None | 數(shù)據(jù)庫(kù)用戶(hù)名 |
| password=‘’ | 數(shù)據(jù)庫(kù)用戶(hù)密碼 |
| database=None | 要連接的數(shù)據(jù)庫(kù)名稱(chēng) |
| port=3306 | 端口號(hào),默認(rèn)為3306 |
| charset=‘’ | 要連接的數(shù)據(jù)庫(kù)的字符編碼(可以在終端登陸mysql后使用 \s 查看,如下圖) |
| connect_timeout=10 | 連接數(shù)據(jù)庫(kù)的超時(shí)時(shí)間,默認(rèn)為10 |
| port=3306 | 端口號(hào),默認(rèn)為3306 |

連接完數(shù)據(jù)庫(kù)后,需要?jiǎng)?chuàng)建一個(gè)游標(biāo)對(duì)象,模塊會(huì)通過(guò)游標(biāo)對(duì)象來(lái)執(zhí)行sql語(yǔ)句以及獲取查詢(xún)結(jié)果,接下來(lái)直接通過(guò)代碼展示各方法。
示例:
import pymysql
db = pymysql.connect(
host="localhost",
port=3306,
user='root', #在這里輸入用戶(hù)名
password='888888', #在這里輸入密碼
charset='utf8mb4'
) #連接數(shù)據(jù)庫(kù)
cursor = db.cursor() #創(chuàng)建游標(biāo)對(duì)象
sql = 'show databases' #sql語(yǔ)句
cursor.execute(sql) #執(zhí)行sql語(yǔ)句
one = cursor.fetchone() #獲取一條數(shù)據(jù)
print('one:',one)
many = cursor.fetchmany(3) #獲取指定條數(shù)的數(shù)據(jù),不寫(xiě)默認(rèn)為1
print('many:',many)
all = cursor.fetchall() #獲取全部數(shù)據(jù)
print('all:',all)
cursor.close()
db.close() #關(guān)閉數(shù)據(jù)庫(kù)的連接
運(yùn)行結(jié)果:
one: ('coldbox',)
many: (('coldboxtest',), ('db_student',), ('information_schema',))
all: (('mysql',), ('performance_schema',), ('sys',), ('test',), ('wan',))
從結(jié)果可以看出,fetchone(),fetchmany(size),fetchall() 三個(gè)函數(shù)返回值都是元組,但是fetchone()返回的是單個(gè)元組,另外兩個(gè)返回的都是元組的嵌套。
三、創(chuàng)建和管理數(shù)據(jù)庫(kù)
使用游標(biāo)對(duì)象來(lái)執(zhí)行創(chuàng)建和刪除數(shù)據(jù)庫(kù)的sql語(yǔ)句示例:
import pymysql
db = pymysql.connect(
host="localhost",
port=3306,
user='root', #在這里輸入用戶(hù)名
password='888888', #在這里輸入密碼
charset='utf8mb4'
)
cursor = db.cursor() #創(chuàng)建游標(biāo)對(duì)象
try:
sql = 'show databases'
cursor.execute(sql)
print('未創(chuàng)建數(shù)據(jù)庫(kù)前:',cursor.fetchall()) #獲取創(chuàng)建數(shù)據(jù)庫(kù)前全部數(shù)據(jù)庫(kù)
dbname = 'justtest'
sql = 'create database if not exists %s'%(dbname) #創(chuàng)建數(shù)據(jù)庫(kù)
cursor.execute(sql)
sql = 'show databases'
cursor.execute(sql)
print('創(chuàng)建新的數(shù)據(jù)庫(kù)后:',cursor.fetchall()) #獲取創(chuàng)建數(shù)據(jù)庫(kù)后全部數(shù)據(jù)庫(kù)
sql = 'drop database if exists %s'%(dbname) #刪除數(shù)據(jù)庫(kù)
cursor.execute(sql)
sql = 'show databases'
cursor.execute(sql)
print('刪除新的數(shù)據(jù)庫(kù)后:',cursor.fetchall()) #獲取刪除數(shù)據(jù)庫(kù)后全部數(shù)據(jù)庫(kù)
except Exception as e:
print(e)
db.rollback() #回滾事務(wù)
finally:
cursor.close()
db.close() #關(guān)閉數(shù)據(jù)庫(kù)連接
運(yùn)行結(jié)果:
未創(chuàng)建數(shù)據(jù)庫(kù)前: (('coldbox',), ('coldboxtest',), ('db_student',), ('information_schema',), ('mysql',), ('performance_schema',), ('sys',), ('test',), ('wan',))
創(chuàng)建新的數(shù)據(jù)庫(kù)后: (('coldbox',), ('coldboxtest',), ('db_student',), ('information_schema',), ('justtest',), ('mysql',), ('performance_schema',), ('sys',), ('test',), ('wan',))
刪除新的數(shù)據(jù)庫(kù)后: (('coldbox',), ('coldboxtest',), ('db_student',), ('information_schema',), ('mysql',), ('performance_schema',), ('sys',), ('test',), ('wan',))
四、創(chuàng)建和管理表
使用游標(biāo)對(duì)象來(lái)執(zhí)行創(chuàng)建和管理表的sql語(yǔ)句示例:
import pymysql
db = pymysql.connect(
host="localhost",
port=3306,
user='root', #在這里輸入用戶(hù)名
password='888888', #在這里輸入密碼
charset='utf8mb4',
database='justtest' #指定操作的數(shù)據(jù)庫(kù)
)
cursor = db.cursor() #創(chuàng)建游標(biāo)對(duì)象
try:
tableName = 'user'
sql = 'create table %s (id varchar(20) not null, name varchar(20) not null, primary key(id))'%(tableName)
cursor.execute(sql) #執(zhí)行sql語(yǔ)句,創(chuàng)建表
sql = 'show tables'
cursor.execute(sql)
print('顯示創(chuàng)建的表:',cursor.fetchall()) #顯示創(chuàng)建的表
sql = 'desc %s'%(tableName)
cursor.execute(sql)
print('顯示表結(jié)構(gòu):',cursor.fetchall()) #顯示表結(jié)構(gòu)
except Exception as e:
print(e)
db.rollback() #回滾事務(wù)
finally:
cursor.close()
db.close() #關(guān)閉數(shù)據(jù)庫(kù)連接
運(yùn)行結(jié)果:
顯示創(chuàng)建的表: (('user',),)
顯示表結(jié)構(gòu): (('id', 'varchar(20)', 'NO', 'PRI', None, ''), ('name', 'varchar(20)', 'NO', '', None, ''))
總結(jié)
對(duì)于修改表結(jié)構(gòu),插入,查詢(xún),刪除數(shù)據(jù)等操作,與上面的操作大體一樣,主要是對(duì) sql 語(yǔ)句的編寫(xiě),此處不做贅述。
整體過(guò)程:
連接數(shù)據(jù)庫(kù) -> 創(chuàng)建游標(biāo)對(duì)象 -> 編寫(xiě)sql語(yǔ)句 -> 執(zhí)行sql語(yǔ)句 -> 獲取結(jié)果 -> 關(guān)閉數(shù)據(jù)庫(kù)連接
connect() 函數(shù)常用參數(shù):
| 參數(shù) | 說(shuō)明 |
|---|---|
| dsn | 數(shù)據(jù)源名稱(chēng),給出該參數(shù)表示數(shù)據(jù)庫(kù)依賴(lài) |
| host=None | 數(shù)據(jù)庫(kù)連接地址 |
| user=None | 數(shù)據(jù)庫(kù)用戶(hù)名 |
| password=‘’ | 數(shù)據(jù)庫(kù)用戶(hù)密碼 |
| database=None | 要連接的數(shù)據(jù)庫(kù)名稱(chēng) |
| port=3306 | 端口號(hào),默認(rèn)為3306 |
| charset=‘’ | 要連接的數(shù)據(jù)庫(kù)的字符編碼(可以在終端登陸mysql后使用 \s 查看,如下圖) |
| connect_timeout=10 | 連接數(shù)據(jù)庫(kù)的超時(shí)時(shí)間,默認(rèn)為10 |
| port=3306 | 端口號(hào),默認(rèn)為3306 |
connect() 函數(shù)返回的連接對(duì)象的方法總結(jié):
| 方法名 | 說(shuō)明 |
|---|---|
| close() | 關(guān)閉數(shù)據(jù)庫(kù)的連接 |
| commit() | 提交事務(wù) |
| rollback() | 回滾事務(wù) |
| cursor() | 獲取游標(biāo)對(duì)象,操作數(shù)據(jù)庫(kù),如執(zhí)行DML操作,調(diào)用存儲(chǔ)過(guò)程等 |
游標(biāo)對(duì)象的方法:
| 方法名 | 說(shuō)明 |
|---|---|
| callproc(procname,[,parameters]) | 調(diào)用存儲(chǔ)過(guò)程,需要數(shù)據(jù)庫(kù)支持 |
| close() | 關(guān)閉當(dāng)前游標(biāo) |
| execute(operation,[,parameters]) | 執(zhí)行數(shù)據(jù)庫(kù)操作,sql語(yǔ)句或者數(shù)據(jù)庫(kù)命令 |
| executemany(operation, seq_of_params) | 用于批量操作 |
| fetchone() | 獲取查詢(xún)結(jié)果集合中的下一條記錄 |
| fetchmany(size) | 獲取指定數(shù)量的記錄 |
| fetchall() | 獲取查詢(xún)結(jié)果集合所有記錄 |
| nextset() | 跳至下一個(gè)可用的數(shù)據(jù)集 |
| arraysize | 指定使用fetchmany()獲取的行數(shù),默認(rèn)為1 |
| setinputsizes(size) | 設(shè)置調(diào)用execute*()方法時(shí)分配的內(nèi)存區(qū)域大小 |
| setoutputsizes(size) | 設(shè)置列緩沖區(qū)大小,對(duì)大數(shù)據(jù)列尤其有用 |
以上就是Python數(shù)據(jù)庫(kù)編程之pymysql詳解的詳細(xì)內(nèi)容,更多關(guān)于Python pymysql的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
tensorflow實(shí)現(xiàn)簡(jiǎn)單的卷積神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家詳細(xì)介紹了tensorflow實(shí)現(xiàn)簡(jiǎn)單的卷積神經(jīng)網(wǎng)絡(luò),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Python異步執(zhí)行CMD命令的具體實(shí)現(xiàn)
異步執(zhí)行CMD命令是提高Python程序性能的有效方法,本文就來(lái)介紹一下Python異步執(zhí)行CMD命令的具體實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
Python竟能畫(huà)這么漂亮的花,帥呆了(代碼分享)
這篇文章主要介紹了用Python作圖的一個(gè)簡(jiǎn)單實(shí)例,通過(guò)turtle模塊實(shí)現(xiàn)作圖,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Python編程中實(shí)現(xiàn)迭代器的一些技巧小結(jié)
只談迭代器的話在Python中只是一個(gè)泛指的概念,具體的可以用yield、生成器表達(dá)式、iter等多種方式來(lái)構(gòu)建,這里我們整理了Python編程中實(shí)現(xiàn)迭代器的一些技巧小結(jié):2016-06-06
對(duì)python pandas中 inplace 參數(shù)的理解
這篇文章主要介紹了對(duì)python pandas中 inplace 參數(shù)的理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
10款最佳Python開(kāi)發(fā)工具推薦,每一款都是神器
這篇文章主要介紹了10款最佳Python開(kāi)發(fā)工具推薦,每一款都是神器,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2020-10-10

