MySQL適配器PyMySQL詳解
本文我們?yōu)榇蠹医榻B Python3 使用 PyMySQL 連接數(shù)據(jù)庫(kù),并實(shí)現(xiàn)簡(jiǎn)單的增刪改查。
什么是 PyMySQL?
PyMySQL 是在 Python3.x 版本中用于連接 MySQL 服務(wù)器的一個(gè)庫(kù),Python2中則使用mysqldb。
PyMySQL 遵循 Python 數(shù)據(jù)庫(kù) API v2.0 規(guī)范,并包含了 pure-Python MySQL 客戶端庫(kù)。
PyMySQL 安裝
在使用 PyMySQL 之前,我們需要確保 PyMySQL 已安裝。
PyMySQL 下載地址:https://github.com/PyMySQL/PyMySQL。
如果還未安裝,我們可以使用以下命令安裝最新版的 PyMySQL:
$ pip install PyMySQL
如果你的系統(tǒng)不支持 pip 命令,可以使用以下方式安裝:
1、使用 git 命令下載安裝包安裝(你也可以手動(dòng)下載):
$ git clone https://github.com/PyMySQL/PyMySQL $ cd PyMySQL/ $ python3 setup.py install
2、數(shù)據(jù)庫(kù)操作實(shí)例,直接上代碼。
import pymysql
import datainfo
import time
#獲取參數(shù)
host = datainfo.host
username = datainfo.username
password = datainfo.password
database = datainfo.db
print()
#測(cè)試數(shù)據(jù)庫(kù)連接
def testconnect():
#打開數(shù)據(jù)庫(kù)鏈接
db = pymysql.connect(host,username,password,database)
#使用cursor() 方法創(chuàng)建一個(gè)游標(biāo)對(duì)象 cursor
cursor = db.cursor()
#使用execute()方法執(zhí)行SQL查詢
cursor.execute("select version()")
#使用fetchone ()獲取單條數(shù)據(jù)
data = cursor.fetchone()
print(data)
db.close()
#插入數(shù)據(jù)庫(kù)
def InsertDate():
#打開數(shù)據(jù)庫(kù)鏈接
db = pymysql.connect(host,username,password,database,charset='utf8')
#使用cursor() 方法創(chuàng)建一個(gè)游標(biāo)對(duì)象 cursor
cursor = db.cursor()
create_time = time.strftime('%Y-%m-%d %H:%M:%S')
update_time = time.strftime('%Y-%m-%d %H:%M:%S')
start_time = time.strftime('%Y-%m-%d %H:%M:%S')
end_time = time.strftime('%Y-%m-%d %H:%M:%S')
remark = "測(cè)試插入信息"
print("開始")
#Sql 插入語(yǔ)句
sql = "insert into demo(start_time,end_time,creat_time,update_time,remark) " \
"VALUES ('%s','%s','%s','%s','%s')"\
%(start_time,end_time,create_time,update_time,remark)
try:
#執(zhí)行sql
print("執(zhí)行插入")
tt = cursor.execute(sql)
print(tt)
db.commit()
except UnicodeEncodeError as e :
#發(fā)生錯(cuò)誤時(shí)回滾
print(e)
db.rollback()
db.close()
#查詢操作
def selectData():
db = pymysql.connect(host, username, password, database, charset='utf8')
# 使用cursor() 方法創(chuàng)建一個(gè)游標(biāo)對(duì)象 cursor
cursor = db.cursor()
sql = "select * from demo where id >='%d'" %(1)
try:
#執(zhí)行sql
print("執(zhí)行查詢")
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
id = row[0]
start_time = row[1]
end_time = row[2]
create_time = row[3]
update_time = row[4]
remark = row[5]
#打印結(jié)果
print("id = %d,start_time=%s,end_time=%s,create_time=%s,update_time=%s,remark=%s" %(id,start_time,end_time,create_time,update_time,remark))
db.commit()
except UnicodeEncodeError as e :
#發(fā)生錯(cuò)誤時(shí)回滾
print(e)
db.close()
#更新操作
def update_data():
db = pymysql.connect(host, username, password, database, charset='utf8')
# 使用cursor() 方法創(chuàng)建一個(gè)游標(biāo)對(duì)象 cursor
cursor = db.cursor()
update_time = time.strftime('%Y-%m-%d %H:%M:%S')
sql = "update demo set update_time ='%s' where id >='%d' " %(update_time,1)
try:
#執(zhí)行sql
print("執(zhí)行更新")
cursor.execute(sql)
db.commit()
except UnicodeEncodeError as e :
#發(fā)生錯(cuò)誤時(shí)回滾
print(e)
db.rollback()
db.close()
#刪除操作
def delete_Date():
db = pymysql.connect(host, username, password, database, charset='utf8')
# 使用cursor() 方法創(chuàng)建一個(gè)游標(biāo)對(duì)象 cursor
cursor = db.cursor()
sql = "delete from demo where id <'%d' " %(1)
try:
#執(zhí)行sql
print("執(zhí)行刪除")
cursor.execute(sql)
db.commit()
except UnicodeEncodeError as e :
#發(fā)生錯(cuò)誤時(shí)回滾
print(e)
db.rollback()
db.close()
if __name__ == '__main__':
testconnect()
InsertDate()
selectData()
update_data()
delete_Date()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Pygame實(shí)戰(zhàn)之實(shí)現(xiàn)經(jīng)典外星人游戲
這篇文章主要介紹了通過Pygame實(shí)現(xiàn)經(jīng)典的外星人游戲的示例代碼,文中的代碼講解詳細(xì),對(duì)我們了解Pygame有一定的幫助,感興趣的同學(xué)可以試一試2022-01-01
Python+OpenCV六種實(shí)時(shí)圖像處理詳細(xì)講解
OpenCV常用的圖像處理為閾值二值化、邊緣檢測(cè)、輪廓檢測(cè)、高斯濾波、色彩轉(zhuǎn)換、調(diào)節(jié)對(duì)比度。本文主要介紹了利用Python和OpenCV對(duì)實(shí)時(shí)圖像進(jìn)行上述六種操作的詳細(xì)講解,感興趣的可以了解一下。2021-11-11
利用python模擬實(shí)現(xiàn)POST請(qǐng)求提交圖片的方法
最近在利用python做接口測(cè)試,其中有個(gè)上傳圖片的接口,在網(wǎng)上各種搜索,各種嘗試。下面這篇文章主要給大家介紹了關(guān)于利用python模擬實(shí)現(xiàn)POST請(qǐng)求提交圖片的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07
python裝飾器相當(dāng)于函數(shù)的調(diào)用方式
今天小編就為大家分享一篇python裝飾器相當(dāng)于函數(shù)的調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-12-12
python實(shí)現(xiàn)智能語(yǔ)音天氣預(yù)報(bào)
今天小編就為大家分享一篇python實(shí)現(xiàn)智能語(yǔ)音天氣預(yù)報(bào),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-12-12
python實(shí)現(xiàn)無(wú)人機(jī)航拍圖片像素坐標(biāo)轉(zhuǎn)世界坐標(biāo)的示例代碼
已知相機(jī)參數(shù)在給定像素坐標(biāo)的前提下,求世界坐標(biāo),大部分通過AI來(lái)實(shí)現(xiàn),本文給大家分享實(shí)現(xiàn)腳本,感興趣的朋友跟隨小編一起看看吧2024-06-06

