我试图在 python 2.7(windows) 中运行以下命令,但出现错误:
ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit byte strings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
#!/usr/bin/python
# -*- encoding: utf-8-*-
import sqlite3;
db = sqlite3.connect('db.sqlite3')
cursor = db.cursor()
cursor.execute('INSERT INTO X(id,a,b) VALUES(?,?,?)', (999998,"ą---ź---ć---ó---ęĘ","ssf"))
cursor.execute('SELECT * FROM X where id = 999998')
for row in cursor:
print row
db.commit()
db.close()
如何将这些字符串(“ąźć”= 波兰符号)插入到我的 sqlite 数据库中?
最佳答案
您可以遵循他们的建议:“只需将您的应用程序切换到 Unicode 字符串即可。”
cursor.execute(
'INSERT INTO X(id,a,b) VALUES(?,?,?)',
(999998,u"ą---ź---ć---ó---ęĘ","ssf"))
注意字符串文字前的 u
。这使它成为一个 unicode 文字。
关于python - 编程错误 : You must not use 8-bit bytestrings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29662004/