我想将unicode文本插入到mysql表中,所以为此我编写了下面的代码
我正在使用flask框架
import MySQLdb as ms
db = ms.connect("localhost","username","password","dbname")
cursor = db.cursor()
my_text = "का" #this is marathi lang word
enc = my_text.encode('utf-8') #after encoding still it shows me marathi word
db_insert = "INSERT INTO TEXT(text) VALUES '{0}'"
cursor.execute(db_insert,(enc))
db.commit()
它给了我以下错误
TypeError: not all arguments converted during string formatting on line cursor.execute()
如何消除这个错误?
最佳答案
将其放在源代码的开头:
# -*- coding: utf-8 -*-
并且不要对已经编码的内容进行编码 - 删除 my_text.encode('utf-8')
在连接调用中使用charset="utf8", use_unicode=True
。
表/列中的字符集
必须是utf8
或utf8mb4
。 latin1
将无法正常工作。
关于python - 使用python在mysql中插入unicode值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37043872/