標籤: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 互動