先下载pymysql文件,http://webscripts.softpedia.com/script/Database-Tools/PyMySQL-71606.html
我下载的是:PyMySQL-0.7.4.zip
然后解压:$ unzip PyMySQL-0.7.4.zip
重命名:mv PyMySQL-0.7.4 py3mysql
登陆root,给该文件夹授权,否则安装不成功,# chmod -R 777 py3mysql
进入py3mysql # cd py3mysql
运行setup.py进行安装:# python3 setup.py install
如果出现类似语句,说明安装成功。
Processing dependencies for PyMySQL==0.7.4
Finished processing dependencies for PyMySQL==0.7.4
接下来进行测试:
# python3 回车,进入python3
# 导入MySQL驱动: >>> import mysql.connector
# 注意把password设为你的root口令:
>>> conn = mysql.connector.connect(user='root', password='password', database='test')
>>> cursor = conn.cursor()
# 创建user表:
>>> cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
# 插入一行记录,注意MySQL的占位符是%s:
>>> cursor.execute('insert into user (id, name) values (%s, %s)', ['1', 'Michael'])
>>> cursor.rowcount
1
# 提交事务:
>>> conn.commit() >>> cursor.close()
# 运行查询:
>>> cursor = conn.cursor()
>>> cursor.execute('select * from user where id = %s', ['1'])
>>> values = cursor.fetchall()
>>> values [('1', 'Michael')]
# 关闭Cursor和Connection:
>>> cursor.close()
True
>>> conn.close()
至此,ubuntu上的mysql数据库就能够支持python3.*