我需要使用Python3
脚本,在MySQL 5.7数据库中插入带有几个非ASCII字符的字符串,比如è
或…
甚至其他字母。我转换了utf8mb4
中感兴趣的表的所有列。你知道吗
连接
db1 = MySQLdb.connect (
host="host1",
user="user1",
passwd="secret",
db="db1"
)
cursor1 = db1.cursor()
cursor1.execute("USE db1")
我可以用è
正确地存储字符串。带有…
的字符串将生成以下错误:
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 1022: ordinal not in range(256)
反之亦然,连接
db1 = MySQLdb.connect (
host="host1",
user="user1",
passwd="secret",
db="db1"
)
cursor1 = db1.cursor()
cursor1.execute("USE db1")
cur.execute("SET NAMES utf8mb4;")
cur.execute("SET CHARACTER SET utf8mb4;")
cur.execute("SET character_set_connection=utf8mb4;")
生成由è
引起的错误:
_mysql_exceptions.OperationalError: (1366, "Incorrect string value: '\\xE8 string...' for column 'column1' at row 1")
è
具有十六进制代码E8
。你知道吗
怎么了?你知道吗
在连接到DB时,您应该尝试通过如下操作将编码设置为UTF8:
相关问题 更多 >
编程相关推荐