日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python和mysql交互操作實例詳解【基于pymysql庫】

系統(tǒng) 1862 0

本文實例講述了python和mysql交互操作。分享給大家供大家參考,具體如下:

python要和mysql交互,我們利用 pymysql 這個庫。

下載地址:
https://github.com/PyMySQL/PyMySQL

安裝(注意cd到我們項目的虛擬環(huán)境后):

            
cd 項目根目錄/abc/bin/
#執(zhí)行
./python3 -m pip install pymysql


          

稍等片刻,就會把 pymysql 庫下載到項目虛擬環(huán)境abc/lib/python3.5/site-packages中。(注意我項目是這個路徑,你的不一定)

文檔地址:
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': '本機新聞標題'}
finally:
  connection.close()


          

我們連上了本地數(shù)據(jù)庫test,從news表中取數(shù)據(jù),數(shù)據(jù)結(jié)果為 {'id': 1, 'title': '本機新聞標題'}

返回的結(jié)果是字典類型,這是因為在連接數(shù)據(jù)庫的時候我們是這樣設(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

字典游標,所以結(jié)果集是字典類型。

我們修改為如下:

            
cursorclass=pymysql.cursors.Cursor


          

結(jié)果集如下:

(1, '本機新聞標題')

變成了元組類型。我們還是喜歡字典類型,因為其中包含了表字段。

Cursor對象

主要有4種:

Cursor 默認,查詢返回list或者tuple
DictCursor? 查詢返回dict,包含字段名
SSCursor??? 效果同Cursor,無緩存游標
SSDictCursor 效果同DictCursor,無緩存游標。

插入

            
try:
  with connection.cursor() as cursor:
    sql = "INSERT INTO news(`title`)VALUES (%s)"
    cursor.execute(sql,["今天的新聞"])
  # 手動提交 默認不自動提交
  connection.commit()
finally:
  connection.close()


          

一次性插入多條數(shù)據(jù)

            
try:
  with connection.cursor() as cursor:
    sql = "INSERT INTO news(`title`)VALUES (%s)"
    cursor.executemany(sql,["新聞標題1","新聞標題2"])
  # 手動提交 默認不自動提交
  connection.commit()
finally:
  connection.close()


          

注意 executemany() 有別于 execute()

sql綁定參數(shù)

            
sql = "INSERT INTO news(`title`)VALUES (%s)"
cursor.executemany(sql,["新聞標題1","新聞標題2"])


          

我們用 %s 占位,執(zhí)行SQL的時候才傳遞具體的值。上面我們用的是list類型:

["新聞標題1","新聞標題2"]

可否用元組類型呢?

            
cursor.executemany(sql,("元組新聞1","元組新聞2"))


          

同樣成功插入到數(shù)據(jù)表了。

把前面分析得到的基金數(shù)據(jù)入庫

創(chuàng)建一個基金表:

            
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='基金表';


          

準備插入SQL:

復(fù)制代碼 代碼如下:
INSERT INTO fund(`code`,`name`,`NAV`,`ACCNAV`,`updated_at`)VALUES (%(code)s,%(name)s,%(NAV)s,%(ACCNAV)s,%(updated_at)s)

注意 %(code)s 這種占位符,要求我們執(zhí)行這SQL的時候傳入的參數(shù)必須是字典數(shù)據(jù)類型。

MySQL小知識:

在插入的時候如果有重復(fù)的主鍵,就更新

            
insert into 表名 xxxx ON duplicate Key update 表名


          

我們這里要準備執(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 = () # 初始化一個元組
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ù)存放在一個 result 的元組里了。

我們打印這個 result 可以看到:

復(fù)制代碼 代碼如下:
({'code': '004223', 'ACCNAV': '1.6578', 'name': '金信多策略精選靈活配置', 'NAV': '1.6578'}, ...}

元組里每個元素 都是字典。

看字典是不是我們數(shù)據(jù)表的字段能對應(yīng)了,但還少一個 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 = () # 初始化一個元組
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)
  # 手動提交 默認不自動提交
  connection.commit()
finally:
  connection.close()


          

python和mysql交互操作實例詳解【基于pymysql庫】_第1張圖片

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 = () # 初始化一個元組
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)
  # 手動提交 默認不自動提交
  connection.commit()
finally:
  connection.close()


          

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python數(shù)學(xué)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設(shè)計有所幫助。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 林周县| 沿河| 镇康县| 巴塘县| 芜湖县| 宜州市| 喀什市| 精河县| 邹城市| 怀集县| 星子县| 静宁县| 宜章县| 平原县| 论坛| 长葛市| 无极县| 眉山市| 绥中县| 兴文县| 南宫市| 东山县| 元江| 南城县| 会泽县| 敖汉旗| 宜宾市| 临桂县| 富顺县| 佛坪县| 定襄县| 徐汇区| 凤凰县| 瑞丽市| 仲巴县| 河津市| 寻甸| 贺州市| 新巴尔虎右旗| 华宁县| 芜湖市|