MySQLdb无法初始化字符集utf8

2024-04-25 21:08:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图使用MySQLdb驱动程序在数据库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字符,但是它似乎忽略了这一点。

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


Tags: 数据库trueconnect驱动程序connutfwordmysqldb

热门问题