python和mysql交互操作實(shí)例詳解【基于pymysql庫(kù)】
本文實(shí)例講述了python和mysql交互操作。分享給大家供大家參考,具體如下:
python要和mysql交互,我們利用pymysql這個(gè)庫(kù)。
下載地址:
https://github.com/PyMySQL/PyMySQL
安裝(注意cd到我們項(xiàng)目的虛擬環(huán)境后):
cd 項(xiàng)目根目錄/abc/bin/ #執(zhí)行 ./python3 -m pip install pymysql
稍等片刻,就會(huì)把pymysql庫(kù)下載到項(xiàng)目虛擬環(huán)境abc/lib/python3.5/site-packages中。(注意我項(xiàng)目是這個(gè)路徑,你的不一定)
文檔地址:
http://pymysql.readthedocs.io/en/latest/
使用:
import pymysql.cursors
# 連接數(shù)據(jù)庫(kù)
connection = pymysql.connect(host='localhost',
user='root',
password='root',
db='test',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT * From news"
cursor.execute(sql)
result = cursor.fetchone()
print(result) # {'id': 1, 'title': '本機(jī)新聞標(biāo)題'}
finally:
connection.close()
我們連上了本地?cái)?shù)據(jù)庫(kù)test,從news表中取數(shù)據(jù),數(shù)據(jù)結(jié)果為{'id': 1, 'title': '本機(jī)新聞標(biāo)題'}

返回的結(jié)果是字典類型,這是因?yàn)樵谶B接數(shù)據(jù)庫(kù)的時(shí)候我們是這樣設(shè)置的:
# 連接數(shù)據(jù)庫(kù)
connection = pymysql.connect(host='localhost',
user='root',
password='root',
db='test',
charset='utf8mb4',
cursorclass=pymysql.cursors.Cursor)
我們把cursorclass設(shè)置的是:pymysql.cursors.DictCursor。
字典游標(biāo),所以結(jié)果集是字典類型。
我們修改為如下:
cursorclass=pymysql.cursors.Cursor
結(jié)果集如下:
(1, '本機(jī)新聞標(biāo)題')
變成了元組類型。我們還是喜歡字典類型,因?yàn)槠渲邪吮碜侄巍?/p>
Cursor對(duì)象
主要有4種:
Cursor 默認(rèn),查詢返回list或者tuple
DictCursor 查詢返回dict,包含字段名
SSCursor 效果同Cursor,無(wú)緩存游標(biāo)
SSDictCursor 效果同DictCursor,無(wú)緩存游標(biāo)。
插入
try:
with connection.cursor() as cursor:
sql = "INSERT INTO news(`title`)VALUES (%s)"
cursor.execute(sql,["今天的新聞"])
# 手動(dòng)提交 默認(rèn)不自動(dòng)提交
connection.commit()
finally:
connection.close()
一次性插入多條數(shù)據(jù)
try:
with connection.cursor() as cursor:
sql = "INSERT INTO news(`title`)VALUES (%s)"
cursor.executemany(sql,["新聞標(biāo)題1","新聞標(biāo)題2"])
# 手動(dòng)提交 默認(rèn)不自動(dòng)提交
connection.commit()
finally:
connection.close()
注意executemany()有別于execute()。
sql綁定參數(shù)
sql = "INSERT INTO news(`title`)VALUES (%s)" cursor.executemany(sql,["新聞標(biāo)題1","新聞標(biāo)題2"])
我們用%s占位,執(zhí)行SQL的時(shí)候才傳遞具體的值。上面我們用的是list類型:
["新聞標(biāo)題1","新聞標(biāo)題2"]
可否用元組類型呢?
cursor.executemany(sql,("元組新聞1","元組新聞2"))
同樣成功插入到數(shù)據(jù)表了。
把前面分析得到的基金數(shù)據(jù)入庫(kù)
創(chuàng)建一個(gè)基金表:
CREATE TABLE `fund` ( `code` varchar(50) NOT NULL, `name` varchar(255), `NAV` decimal(5,4), `ACCNAV` decimal(5,4), `updated_at` datetime, PRIMARY KEY (`code`) ) COMMENT='基金表';
準(zhǔn)備插入SQL:
注意%(code)s這種占位符,要求我們執(zhí)行這SQL的時(shí)候傳入的參數(shù)必須是字典數(shù)據(jù)類型。
MySQL小知識(shí):
在插入的時(shí)候如果有重復(fù)的主鍵,就更新
insert into 表名 xxxx ON duplicate Key update 表名
我們這里要準(zhǔn)備執(zhí)行的SQL就變成這樣了:
INSERT INTO fund(code,name,NAV,ACCNAV,updated_at)VALUES (%(code)s,%(name)s,%(NAV)s,%(ACCNAV)s,%(updated_at)s) ON duplicate Key UPDATE updated_at=%(updated_at)s,NAV=%(NAV)s,ACCNAV=%(ACCNAV)s;
1、回顧我們前面分析處理的基金網(wǎng)站數(shù)據(jù)
//chabaoo.cn/article/162452.htm
#...
codes = soup.find("table",id="oTable").tbody.find_all("td","bzdm")
result = () # 初始化一個(gè)元組
for code in codes:
result += ({
"code":code.get_text(),
"name":code.next_sibling.find("a").get_text(),
"NAV":code.next_sibling.next_sibling.get_text(),
"ACCNAV":code.next_sibling.next_sibling.next_sibling.get_text()
},)
最后我們是把數(shù)據(jù)存放在一個(gè)result的元組里了。
我們打印這個(gè)result可以看到:
元組里每個(gè)元素 都是字典。
看字典是不是我們數(shù)據(jù)表的字段能對(duì)應(yīng)了,但還少一個(gè)updated_at字段的數(shù)據(jù)。
2、我們把分析的網(wǎng)頁(yè)數(shù)據(jù)重新處理一下
from datetime import datetime
updated_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
result = () # 初始化一個(gè)元組
for code in codes:
result += ({
"code":code.get_text(),
"name":code.next_sibling.find("a").get_text(),
"NAV":code.next_sibling.next_sibling.get_text(),
"ACCNAV":code.next_sibling.next_sibling.next_sibling.get_text(),
"updated_at":updated_at
},)
3、最后插入的代碼
try:
with connection.cursor() as cursor:
sql = """INSERT INTO fund(`code`,`name`,`NAV`,`ACCNAV`,`updated_at`)VALUES (%(code)s,%(name)s,%(NAV)s,%(ACCNAV)s,%(updated_at)s)
ON duplicate Key UPDATE `updated_at`=%(updated_at)s,`NAV`=%(NAV)s,`ACCNAV`=%(ACCNAV)s"""
cursor.executemany(sql,result)
# 手動(dòng)提交 默認(rèn)不自動(dòng)提交
connection.commit()
finally:
connection.close()

4、完整的分析html內(nèi)容(基金網(wǎng)站網(wǎng)頁(yè)內(nèi)容),然后插入數(shù)據(jù)庫(kù)代碼:
from bs4 import BeautifulSoup
import pymysql.cursors
from datetime import datetime
# 讀取文件內(nèi)容
with open("1.txt", "rb") as f:
html = f.read().decode("utf8")
f.close()
# 分析html內(nèi)容
soup = BeautifulSoup(html,"html.parser")
# 所有基金編碼
codes = soup.find("table",id="oTable").tbody.find_all("td","bzdm")
updated_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
result = () # 初始化一個(gè)元組
for code in codes:
result += ({
"code":code.get_text(),
"name":code.next_sibling.find("a").get_text(),
"NAV":code.next_sibling.next_sibling.get_text(),
"ACCNAV":code.next_sibling.next_sibling.next_sibling.get_text(),
"updated_at":updated_at
},)
# 連接數(shù)據(jù)庫(kù)
connection = pymysql.connect(host='localhost',
user='root',
password='root',
db='test',
charset='utf8mb4',
cursorclass=pymysql.cursors.Cursor)
try:
with connection.cursor() as cursor:
sql = """INSERT INTO fund(`code`,`name`,`NAV`,`ACCNAV`,`updated_at`)VALUES (%(code)s,%(name)s,%(NAV)s,%(ACCNAV)s,%(updated_at)s)
ON duplicate Key UPDATE `updated_at`=%(updated_at)s,`NAV`=%(NAV)s,`ACCNAV`=%(ACCNAV)s"""
cursor.executemany(sql,result)
# 手動(dòng)提交 默認(rèn)不自動(dòng)提交
connection.commit()
finally:
connection.close()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫(kù)操作技巧匯總》、《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python 高效去重復(fù) 支持GB級(jí)別大文件的示例代碼
今天小編就為大家分享一篇python 高效去重復(fù) 支持GB級(jí)別大文件的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
django drf框架自帶的路由及最簡(jiǎn)化的視圖
這篇文章主要介紹了django-drf框架自帶的路由以及最簡(jiǎn)化的視圖,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
Python之torch.no_grad()函數(shù)使用和示例
這篇文章主要介紹了Python之torch.no_grad()函數(shù)使用和示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
python實(shí)現(xiàn)自動(dòng)獲取IP并發(fā)送到郵箱
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)自動(dòng)獲取IP并發(fā)到郵箱,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Pandas庫(kù)中isnull函數(shù)的實(shí)現(xiàn)
isnull()是Pandas庫(kù)中DataFrame和Series對(duì)象的一個(gè)函數(shù),用于檢測(cè)數(shù)據(jù)中的缺失值,本文主要介紹了Pandas庫(kù)中isnull函數(shù)的實(shí)現(xiàn),具有一定參考價(jià)值,感興趣的可以了解一下2024-07-07

