1. 下载PyMysql并且安装
下载zip包后解压到目录,进入该目录,执行以下命令安装
python setup.py install
2. 编写一个简单的数据库访问程序
simple_mysql.py
import pymysql conn = pymysql.connect(host='127.0.0.1', user='root', passwd='yourpassword', db='mysql', port=3306, charset='utf8') cur = conn.cursor() cur.execute("USE YourDatabase") cur.execute("SELECT * FROM YourTable") print(cur.fetchone()) cur.close() conn.close()
3. 执行脚本
python simple_mysql.py