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

标签 python mysql utf-8 character-encoding iso-8859-1

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

我遇到了 latin-1 编码错误。但是在阅读之后,我发现 MySQLdb 驱动程序默认为 latin-1 并且我必须在 显式设置 utf-8 作为我选择的字符集>mariadb.connect() 函数。 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 字符,但它似乎忽略了这一点。

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

最佳答案

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/

相关文章:

python - 比较 PostgreSQL 中具有 "ORDER BY"的 2 个查询的结果以检查匹配和不匹配

php - MySQL更新查询不改变数据库中的值

mysql插入选择后删除

php - 是否可以在 Laravel 中获取数据透视表与另一个表的关系?

Python + PostgreSQL + 奇怪的ascii = UTF8编码错误

python - 从数据存储实体获取键名

python - Pandas Series.filter.values 返回与 numpy 数组不同的类型

r - R Studio 中的汉字编码

c# - 将问题 ANSI 转换为 UTF8 C#

python - 分割字符串但保留 "split"字符