我正在尝试使用 MySQLdb 驱动程序将一些阿拉伯语单词插入到我的 hanswehr2
数据库 Maria DB 的 arabic_word
列中。
我遇到了 latin-1 编码错误
。但是在阅读之后,我发现 MySQLdb 驱动程序默认为 latin-1
并且我必须在 显式设置
函数。 Sauce. utf-8
作为我选择的字符集>mariadb.connect()
整个数据库设置为utf-8。
代码:
def insert_into_db(arabic_word, definition):
try:
conn = mariadb.connect('localhost', 'root', 'xyz1234passwd', 'hans_wehr', charset='utf-8', use_unicode=True)
conn.autocommit(True)
cur = conn.cursor()
cur.execute("INSERT INTO hanswehr2 (arabic_word , definition) VALUES (%s,%s)", (arabic_word, definition,))
except mariadb.Error, e:
print e
sys.exit(1)
但是现在我得到以下错误:
/usr/bin/python2.7 /home/heisenberg/hans_wehr/main.py
Total lines 87672
(2019, "Can't initialize character set utf-8 (path: /usr/share/mysql/charsets/)")
Process finished with exit code 1
我已指定 Python MySQL 驱动程序使用 utf-8 字符,但它似乎忽略了这一点。
如有任何意见,我们将不胜感激。
最佳答案
MySQL 中 UTF-8 的字符集别名是 utf8
(没有连字符)。
参见 https://dev.mysql.com/doc/refman/5.5/en/charset-charsets.html可用的字符集。
请注意,如果您需要使用非 BMP Unicode 点,例如表情符号,请使用 utf8mb4
作为连接字符集和 varchar 类型。
关于python - MySQLdb无法初始化字符集utf-8错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37709659/