1.'latin-1' codec can't encode characters错误
首先要确保pymysql的配置文件编码为utf8,否则就会报出以下错误
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 9-13: ordinal not in range(256)
配置文件在pymysql模块对应的安装目录,我的
Linux下为 /usr/local/lib/python3.5/dist-packages/pymysql
Windows下为 D:\Python35\Lib\site-packages/pymysql
我们打开该目录,会发现一个名为connections.py的文件,然后我们通过编辑器打开文件,通过搜索"charset=",发现如下所示的代码:
def __init__(self, host=None, user=None, password="",
database=None, port=0, unix_socket=None,
charset='', sql_mode=None,
read_default_file=None, conv=None, use_unicode=None,
client_flag=0, cursorclass=Cursor, init_command=None,
connect_timeout=10, ssl=None, read_default_group=None,
compress=None, named_pipe=None, no_delay=None,
autocommit=False, db=None, passwd=None, local_infile=False,
max_allowed_packet=16*1024*1024, defer_connect=False,
auth_plugin_map={}, read_timeout=None, write_timeout=None,
bind_address=None, binary_prefix=False):
代码中,charset参数可以设置编码,此时我们将编码设置为"utf8"(注意不是utf-8),修改后保存,如下所示:
def __init__(self, host=None, user=None, password="",
database=None, port=0, unix_socket=None,
charset='utf8', sql_mode=None,
read_default_file=None, conv=None, use_unicode=None,
client_flag=0, cursorclass=Cursor, init_command=None,
connect_timeout=10, ssl=None, read_default_group=None,
compress=None, named_pipe=None, no_delay=None,
autocommit=False, db=None, passwd=None, local_infile=False,
max_allowed_packet=16*1024*1024, defer_connect=False,
auth_plugin_map={}, read_timeout=None, write_timeout=None,
bind_address=None, binary_prefix=False):
2.pymysql.err.InternalError:(1366, "Incorrect string value: '\\xE5\\x93\\x88\\xE5\\x93\\x88' for column 'title' at row 1") 错误
修改了pymysql的配置文件之后,可能会遇到pymysql.err.InternalError的错误,出现该错误的原因是mysql表中各列的编码问题,