以下是运行代码段
#!/usr/bin/python
# coding: utf-8
import pymysql
cxn = pymysql.Connect(host = 'localhost', port = 3306 , user = 'root', passwd = '123456', db='test',charset='utf8')
cur = cxn.cursor()
sql = """
CREATE TABLE USER1 (
id INT auto_increment PRIMARY KEY ,
name CHAR(10) NOT NULL UNIQUE,
age TINYINT NOT NULL
)ENGINE=innodb DEFAULT CHARSET=utf8;
"""
cur.execute(sql)
cur.close()
cxn.commit()
cxn.close()
#报错记录
Traceback (most recent call last):
File "mysql_test.py", line 5, in <module>
cxn = pymysql.Connect(host = 'localhost', port = 3306 , user = 'root', passwd = '123456', db='pop_scpy',charset='utf8')
File "/root/.pyenv/versions/3.4.1/lib/python3.4/site-packages/pymysql/__init__.py", line 93, in Connect
return Connection(*args, **kwargs)
File "/root/.pyenv/versions/3.4.1/lib/python3.4/site-packages/pymysql/connections.py", line 510, in __init__
self._connect()
File "/root/.pyenv/versions/3.4.1/lib/python3.4/site-packages/pymysql/connections.py", line 676, in _connect
self._get_server_information()
File "/root/.pyenv/versions/3.4.1/lib/python3.4/site-packages/pymysql/connections.py", line 830, in _get_server_information
self.server_charset = charset_by_id(self.server_language).name
File "/root/.pyenv/versions/3.4.1/lib/python3.4/site-packages/pymysql/charset.py", line 173, in charset_by_id
return _charsets.by_id(id)
File "/root/.pyenv/versions/3.4.1/lib/python3.4/site-packages/pymysql/charset.py", line 21, in by_id
return self._by_id[id]
KeyError: 45
解决方法 看到一个帖子讲到可能是pymysql的版本过低,需要升级
通过pip list指令 查看pymysql版本
[root local]# pip list
PyMySQL3 (0.5)
[root local]# pip install --upgrade PyMySQL
Collecting PyMySQL
Downloading http://mirrors.aliyun.com/pypi/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
100% |████████████████████████████████| 51kB 2.5MB/s
Installing collected packages: PyMySQL
Successfully installed PyMySQL-0.9.3
You are using pip version 9.0.1, however version 19.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.