1.安装python
去官网下载安装 https://www.python.org/downloads/windows/
2.安装MySQLdb库
(1)下载并安装VC编译器
http://www.microsoft.com/en-us/download/confirmation.aspx?id=44266
(2)下载并安装MySQL的Connector For C
注意:一定要6.0版本的,并且必须安装在默认路径(C盘)
https://dev.mysql.com/downloads/connector/c/6.0.html
(3)用PIP安装
pip install MySQL-Python
3.使用MySQLdb库
# coding:utf-8 import MySQLdb if __name__ == '__main__': conn = MySQLdb.connect(host = '127.0.0.1', port = 3307, user = 'root', passwd = '', db = 'test') cur = conn.cursor() cur.execute('select * from tb_test1') data = cur.fetchall() cur.close() conn.close() print data
运行结果
更多使用方法可以参考地址
http://www.runoob.com/python/python-mysql.html