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

1 投票
1 回答
4964 浏览
提问于 2025-04-16 11:29

我有一个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 个回答

3

使用带有u前缀的字符串字面量。

print u'I am a Unicode string.'

“Python中的Unicode,完全解密”

撰写回答