因为mysqldb目前仅支持到python3.4,因此这里选择pymysql。python
pymysql下载地址:mysql
https://pypi.python.org/packages/source/P/PyMySQL3/PyMySQL3-0.5.tar.gzsql
解压以后,运行cmd,切换到PyMySQL3-0.5目录数据库
输入python setup.py install,执行安装测试
安装完了,测试一下fetch
在py程序中引用 pymysqlurl
import pymysql
执行代码,若是没有报错的话就表示 pymysql安装成功了blog
因为以前已经安装了mysql数据库,因此能够真实测试一下数据库链接get
上图就是mysql数据库中的一个测试数据库表cmd
测试代码以下:
import pymysql conn = pymysql.connect(host="127.0.0.1",port=3306,user="test",passwd="12345",db="test",charset="utf8") cur = conn.cursor() sql = "select * from user" cur.execute(sql) rows = cur.fetchall() #print(rows) for dr in rows: print(dr)
运行结果以下: