好久没用python了今天下了个3.5.2,不同版本差别很大,大家最好看文档,网上很多不大靠谱,说用pymysql3的pymysql链接,但是老师报错,到官网一看:如下
PyMySQL3 0.5
Pure Python MySQL Driver
DO NOT USE THIS. PyMySQL support Python 3 already.
说不要用pymysql3啦,pymysql已经支持python3啦。刚快卸载了,重新下载个pymysql,已测试,果然OK了。
简单查询参考代码
import pymysql conn = pymysql.connect(host='IP', port=3306, user='uname', passwd='youpasswd',db='dbname',charset='utf8') cur = conn.cursor() cur.execute("SELECT * FROM yourtablename") for r in cur: print("row_number:" , (cur.rownumber) ) print("name:"+str(r[0])+" sex:"+str(r[1])+" age:"+str(r[2])) cur.close() conn.close()