python3.5 與 mysql 互動

來源:互聯網
上載者:User

標籤:mysql   開源   python   connector   pymysql   

python與mysql互動,官網是有connertor支援的,Connector/Python現在最高支援到python3.4:

http://dev.mysql.com/downloads/connector/python/

不過3.5可以用開源的pymysql來串連:

https://github.com/PyMySQL/PyMySQL 


簡單的使用sample:

MySql資料庫準備:

use testcreate table testTbl(    tId int not null,    tName nvarchar(20) not null)

Python向mysql中新增資料:

import pymysqladdQuery = "inset into ttt(tId,tName) values(1,‘test A‘);"conn = pymysql.connect(host=‘localhost‘, user=‘lybing‘, password=‘lybing‘, db=‘test‘, port=3306, charset=‘utf8‘)with conn.cursor() as cursor:    affect = curor.execute(addQuery)conn.commit()#很重要,不能忘conn.close()

其它操作就自己好好實驗吧,簡單給出重點:

cur.fetchone() print u‘只查一條記錄‘cur.fetchmany(2)print u‘多條資料:‘cur.fetchall()print u‘所有資料:‘for data in result:    print dataconn.commit()cur.close()conn.close()



本文出自 “lybing” 部落格,請務必保留此出處http://lybing.blog.51cto.com/3286625/1812782

python3.5 與 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.