MySQLdb无法初始化字符集utf-8错误 [英] MySQLdb can't initialize character set utf-8 error

查看:258
本文介绍了MySQLdb无法初始化字符集utf-8错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to insert some Arabic word into the arabic_word column of my hanswehr2 database Maria DB using the MySQLdb driver.

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

I was getting a latin-1 encode error. But after reading around, I found out that the MySQLdb driver was defaulted to latin-1 and I had to explicitly set utf-8 as my charset of choice at the mariadb.connect() function. Sauce.

整个数据库设置为utf-8.

The entire database is set to 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字符,但似乎忽略了这一点.

I have specified the Python MySQL driver to use the utf-8 character however it seems to ignore this.

任何输入都会受到赞赏.

Any inputs would be highly appreciated.

推荐答案

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

The charset alias for UTF-8 in MySQL is utf8 (no hyphen).

请参见 https://dev.mysql.com/doc/refman/5.5/en/charset-charsets.html 以获得可用的字符集.

See https://dev.mysql.com/doc/refman/5.5/en/charset-charsets.html for available charsets.

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

Note, if you need to use non-BMP Unicode points, such as emojis, use utf8mb4 for the connection charset and the varchar type.

这篇关于MySQLdb无法初始化字符集utf-8错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆