版本说明:
- Python3.5
- MySQL5.7
- mysqlclient应该是最新的
>>>
================== RESTART: C:\Users\rHotD\Desktop\test.py ==================
Traceback (most recent call last):
File "C:\Users\rHotD\Desktop\test.py", line 44, in <module>
message_dict['当前学分']))
File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\MySQLdb\cursors.py", line 234, in execute
args = tuple(map(db.literal, args))
File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\MySQLdb\connections.py", line 316, in literal
s = self.escape(o, self.encoders)
File "C:\Users\rHotD\AppData\Local\Programs\Python\Python35\lib\site-packages\MySQLdb\connections.py", line 226, in unicode_literal
return db.string_literal(str(u).encode(unicode_literal.charset))
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-2: ordinal not in range(256)
当向数据库中插入中文数据时报错,在网上查说是数据库设置问题,但是我记得我之前明明设置了utf-8编码的,于是我又去检查了一遍
mysql命令行输入show variables like 'character%'
和show variables like 'collation_%'
检查了一下数据库的字符设置确实没问题…
mysql> show variables like 'character%'
-> ;
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 5.7\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)
mysql> show variables like 'collation_%'
-> ;
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set, 1 warning (0.00 sec)
加上这一行就好啦!!!!