我正在尝试在数据库中插入元组。它不会在代码中给出任何错误。但输出在打印整行时包含一些虚拟字符。输出也被复制到帖子中。请帮助我找出代码中的错误。这是一个大项目的虚拟代码。
代码:
import sqlite3 as sql
def foo():
db = sql.connect('test.db')
db.execute('drop table if exists test')
db.execute('create table test (t1 text, i1 int)')
str = """insert into test(t1,i1) values ('one',1 ) """
db.execute(str)
db.execute('insert into test(t1,i1) values (?, ?)', ('two',2))
db.commit()
cursor = db.execute('select * from test')
for row in cursor:
print row
输出:
(u'one', 1)
(u'two', 2)
如输出所示,代码的预期输出是两个元素的元组。相反,输出中有一些字符“u”。
谢谢
最佳答案
字符串上的 u
前缀表示它是 Unicode string ,您仍然有一个符合预期的二元素元组。
默认情况下,sqlite3 模块将文本返回为 Unicode 字符串。如果您想接收以 utf-8 编码的字节字符串,您可以设置连接的 text_factory
属性为str
。
关于python - 在 python 中从 SQLite 数据库检索值的一些错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13072952/