标签: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 test create table testTbl ( tId int not null, tName nvarchar(20) not null )
Python向mysql中新增数据:
import pymysql addQuery = "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 data conn.commit() cur.close() conn.close()
本文出自 “lybing” 博客,请务必保留此出处http://lybing.blog.51cto.com/3286625/1812782
标签:mysql 开源 python connector pymysql
原文地址:http://lybing.blog.51cto.com/3286625/1812782