Python 3.2: 使用pymysql连接Mysql
在Python 3.2 中连接MySQL的方式有很多种,例如使用mysqldb,pymysql。本文主要介绍使用Pymysql连接MYSQL的步骤
1 安装pymysql
· 确保python3.2已经正确安装
· 从 https://pypi.python.org/pypi/PyMySQL3 下载安装包
· 解压到指定的路径
· 在命令行模式下进入安装目录,cmd 执行 python setup.py install命令
2 SimpleSample
- '''''
- Created onDec 17, 2013
- @author:
- '''
- importpymysql;
- defaccess_mysql():
- conn = pymysql.connect(host='localhost', port=3306,user='root',db='test',charset='utf8')
- cur = conn.cursor()
- cur.execute("SELECT * FROM dictionarylimit 10")
- for r in cur:
- print("row_number:"+str(cur.rownumber))
- print("id:"+str(r[0])+"key:"+str(r[1])+" mean:"+str(r[2]))
- cur.close()
- conn.close()
- if __name__== '__main__':
- access_mysql();

浙公网安备 33010602011771号