开始想先安装MySqlDB的一个插件,在官网下载http://dev.mysql.com/downloads/connector/python/2.0.html了但是安装不上报错:
python version 2.7 required,which was not found in the registry
后来查到MySqlDBz只适用于Python3.5以前,Python3.5的用PyMysql下载地址https://pypi.python.org/pypi/PyMySQL#downloads
选择PyMySQL-0.7.9.tar.gz (md5)源码安装下载:
将PyMySQL-0.7.9.tar.gz 解压目录结果:
用命令到这个目录下执行:
python setup.py install
import pymysql
下面就用代码测试下连接mysql数据库:
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='root', db='ecshop', charset='UTF8')
cur = conn.cursor()
#新增
# cur.execute("insert into tab1(tab1_id,val) VALUES (3,3)");
#查询mysql版本 cur.execute("SELECT VERSION()");
# SQL 查询语句
sql = "SELECT * FROM tab1 ";
cur.execute(sql);
# 获取所有记录列表
results = cur.fetchall();
for row in results:
#print(row,":result");
print('result:', row);
print('result:',row[0]);
cur.close()
conn.close()
最主要还是Python3.5连接数据库的改动,变成了PyMsql看来最新版的Python用起来需要探索的问题还很多呀。