本文實(shí)例講述了python和mysql交互操作。分享給大家供大家參考,具體如下:
python要和mysql交互,我們利用
pymysql
這個(gè)庫。
下載地址:
https://github.com/PyMySQL/PyMySQL
安裝(注意cd到我們項(xiàng)目的虛擬環(huán)境后):
cd 項(xiàng)目根目錄/abc/bin/ #執(zhí)行 ./python3 -m pip install pymysql
稍等片刻,就會(huì)把
pymysql
庫下載到項(xiàng)目虛擬環(huán)境abc/lib/python3.5/site-packages中。(注意我項(xiàng)目是這個(gè)路徑,你的不一定)
文檔地址:
http://pymysql.readthedocs.io/en/latest/
使用:
import pymysql.cursors # 連接數(shù)據(jù)庫 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ù)庫test,從news表中取數(shù)據(jù),數(shù)據(jù)結(jié)果為
{'id': 1, 'title': '本機(jī)新聞標(biāo)題'}
返回的結(jié)果是字典類型,這是因?yàn)樵谶B接數(shù)據(jù)庫的時(shí)候我們是這樣設(shè)置的:
# 連接數(shù)據(jù)庫 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)槠渲邪吮碜侄巍?
Cursor對(duì)象
主要有4種:
Cursor 默認(rèn),查詢返回list或者tuple
DictCursor? 查詢返回dict,包含字段名
SSCursor??? 效果同Cursor,無緩存游標(biāo)
SSDictCursor 效果同DictCursor,無緩存游標(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ù)入庫
創(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ù)
//www.jb51.net/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)頁數(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)頁內(nèi)容),然后插入數(shù)據(jù)庫代碼:
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ù)庫 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ù)庫操作技巧匯總》、《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ì)有所幫助。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
