将(unicode?)文本插入sqlite3数据库时出错

时间:2011-02-08 22:18:08

标签: python unicode sqlite

我有一个我创建的sqlite3数据库,我正在填充db2数据库中的数据。运行以下内容时出错:

for row in db2_connection.execute(query):
    sqlite3_connection.execute("INSERT INTO MY_TABLE VALUES (?, ?, ?)", row)

这是错误:

File "example.py", line 72, in load_micr_db
    sqlite3_connection.execute("INSERT INTO MY_TABLE VALUES (?, ?, ?)", row)
sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a
text_factory that can interpret 8-bit bytestrings (like text_factory = str). 
It is highly recommended that you instead just switch your application to 
Unicode strings.

这听起来很棒,但我不知道如何遵循它。如何“将我的应用程序切换到Unicode字符串”?

我正在使用Python 2.6,pyodbc来查询db2 9,sqlite3

1 个答案:

答案 0 :(得分:3)

使用前缀为u的字符串文字。

print u'I am a Unicode string.'

"Unicode in Python, Completely Demystified"