python -- 串連mysql資料庫

來源:互聯網
上載者:User

標籤:預設   cti   utf8   使用者   exe   包管理器   com   exec   style   

由於python2.x與3.x的不相容性,導致在python3中,不能使用類似在2.x中常用的模組mysqldb來串連mysql資料庫。

在python3.x中, 串連MySQL的方案有:oursql, PyMySQL, myconnpy 等。

我常用的是pymysql。

1、pymysql安裝:

  一般推薦直接用pip3包管理器安裝

pip3 install pymysql

  還可以有其他安裝方法,百度。

2、pymysql引用:

  跟其他模組一樣,在安裝好pymysql之後,在需要的py檔案裡面匯入pymysql,然後就能使用pymysql進行mysql資料庫連接操作。

import pymysql  

3、pymysql應用:

 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3  4 import pymysql 5  6 #串連mysql 7 conn = pymysql.connect( 8     host=‘127.0.0.1‘, 9     port=3306,10     user=‘root‘,11     passwd=‘root‘,12     db=‘pystudy‘,13     charset=‘utf8‘14 )15 16 #使用cursor方法建立一個遊標對象 cur17 cur = conn.cursor()18 19 #使用excute方法執行SQL語句,並返回受影響的行數20 # reCount = cur.execute(‘select * from students‘)21 22 #使用executemany方法執行多條insert語句23 li = [24     (‘txowner‘,‘male‘,23,13281818181,‘CN‘),25     (‘xtsec‘,‘male‘,25,132834321,‘USA‘),26     (‘tom‘,‘male‘,28,1328153431,‘JP‘),27     (‘gouzi‘,‘female‘,22,132345181,‘CN‘),28 ]29 reCount1 = cur.executemany(‘insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)‘,li)30 31 #刪除資料32 # reCount = cur.execute(‘delete from students where age = 28‘)33 34 #修改資料35 # reCount = cur.execute(‘update students set sex = %s where name = %s‘,(‘female‘,‘admin‘))36     #注意:這裡用%s佔位,後面寫參數時不需要用%37 38 #事物復原操作(事物復原操作只有在提交事務之前才有效,一旦事物提交,就不能進行復原了)39 conn.rollback()  #只有在ENGINE=InnoDB時有用,MyISAM不支援事物,所以不能進行復原40 41 #提交當前事物到資料庫執行42 conn.commit()43 44 #使用cursor對象的fetchall方法擷取全部資料45 # data = cur.fetchall()46 # data = cur.fetchone()47 # data = cur.fetchmany(3)48 49 # print(data)50 51 #關閉串連52 cur.close()53 conn.close()

4、pymysql模組說明:

#!/usr/bin/env python# -*- coding: utf-8 -*-‘‘‘pymysql模組作用:python串連mysql簡單用法:pymysql.Connect()參數說明host(str):      MySQL伺服器位址port(int):      MySQL伺服器連接埠號碼(預設3306可不寫)user(str):      使用者名稱passwd(str):    密碼db(str):        資料庫名稱charset(str):   串連編碼(預設可不寫)connection對象支援的方法cursor()        使用該串連建立並返回遊標commit()        提交當前事務rollback()      復原當前事務(mysql使用事務前需確定儲存引擎為innodb,MyISAM是不能進行復原的)close()         關閉串連cursor對象支援的方法execute(op)     執行一個資料庫的操作命令executemany()   執行多個資料庫的操作命令fetchone()      取得結果集的下一行fetchmany(size) 擷取結果集的下幾行(以元組形式返回)fetchall()      擷取結果集中的所有行(以元組形式返回)rowcount()      返回資料條數或影響行數close()         關閉遊標對象‘‘‘

 5、事物復原

 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3  4 import pymysql 5  6 #串連mysql 7 conn = pymysql.connect( 8     host=‘127.0.0.1‘, 9     port=3306,10     user=‘root‘,11     passwd=‘root‘,12     db=‘pystudy‘,13     charset=‘utf8‘14 )15 16 #使用cursor方法建立一個遊標對象 cur17 cur = conn.cursor()18 19 try:20     li = [21         (‘txowner‘,‘male‘,23,13281818181,‘CN‘),22         (‘xtsec‘,‘male‘,25,132834321,‘USA‘),23         (‘tom‘,‘male‘,28,1328153431,‘JP‘),24         (‘gouzi‘,‘female‘,22,132345181,‘CN‘),25     ]26     reCount1 = cur.executemany(‘insert into students(name,sex,age,tel,nal) values(%s,%s,%s,%s,%s)‘,li)27     assert 1==228 29 except Exception as e:30     conn.rollback()31 32 else:33     conn.commit()34 35 #關閉串連36 cur.close()37 conn.close()

 

python -- 串連mysql資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.