MySQLdb无法初始化字符集utf-8错误

MySQLdb can't initialize character set utf-8 error

我正在尝试使用 MySQLdb 驱动程序将一些阿拉伯语单词插入到我的 hanswehr2 数据库 Maria DB 的 arabic_word 列中。

我得到了 latin-1 encode error。但是在阅读之后,我发现 MySQLdb 驱动程序默认为 latin-1,我必须在 mariadb.connect() 函数中明确设置 utf-8 作为我选择的字符集。 Sauce.

整个数据库设置为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 字符,但它似乎忽略了这一点。

如有任何意见,我们将不胜感激。

有一种叫做 collat​​ions 的东西可以帮助 encode/decode 特定语言的字符。 https://softwareengineering.stackexchange.com/questions/95048/what-is-the-difference-between-collation-and-character-set

我认为你需要在创建你的数据库指定 table 连接字符串。参考这个: store arabic in SQL database

有关 python mysql 连接 的更多信息: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-set-charset-collation.html

MySQL 中 UTF-8 的字符集别名是 utf8(无连字符)。

有关可用的字符集,请参阅 https://dev.mysql.com/doc/refman/5.5/en/charset-charsets.html

注意,如果您需要使用非 BMP Unicode 点,例如表情符号,请使用 utf8mb4 作为连接字符集和 varchar 类型。