Python - PyMYSQL遇到的连接报错 KeyError: 45

以下是运行代码段

#!/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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值