我之前参与过一个PHP项目,其中准备好的语句使SELECT查询的速度提高了20%.
我想知道它是否适用于Python?我似乎无法找到任何明确表示它做或不做的事情.
如何在Python中用ut8mb4编码?
我有两组数据:数据我将从Parse迁移到我的新MySQL数据库,而数据将继续(仅与我的新数据库对话).我的数据库是utf8mb4,以存储表情符号和重音字母.
当我在python脚本中时,第一组数据只显示正确(当涉及表情符号和重音符号时):
MySQLdb.escape_string(unicode(xstr(data.get('message'))).encode('utf-8'))
Run Code Online (Sandbox Code Playgroud)
当从PHP中读取MySQL数据库时:
$row["message"] = utf8_encode($row["message"]);
Run Code Online (Sandbox Code Playgroud)
当我不包括该utf8_encode($row["message"])部分时,第二组数据仅正确显示(当涉及表情符号和重音时).我正在尝试协调这些,以便将两组数据正确地返回到我的iOS应用程序.请帮忙!
我试图使用MySQLdb驱动程序将一些阿拉伯语单词插入arabic_word我的hanswehr2数据库Maria DB 的列中.
我得到了一个latin-1 encode error.但在阅读之后,我发现MySQLdb驱动程序是默认的latin-1,我必须在函数中明确设置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)
Run Code Online (Sandbox Code Playgroud)
但是现在我收到以下错误:
/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
Run Code Online (Sandbox Code Playgroud)
我已经指定Python MySQL驱动程序使用utf-8字符,但它似乎忽略了这一点.
任何投入都将受到高度赞赏.