SQLite3:除非使用text_factory,否则不得使用8位字节串

时间:2015-08-25 20:03:52

标签: python sqlite sqlalchemy

为了自学python,我一直在使用whois模块和sqlalchemy

我正在编写一个浏览单词列表的应用程序,并为添加了.tld的每个单词进行查找。这样做效果很好,但是一些数据返回使SQLite抛出以下错误

sqlalchemy.exc.ProgrammingError: (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. [SQL: u'INSERT INTO domains (domain, country, city, updated_date, expiration_date, registrar) VALUES (?, ?, ?, ?, ?, ?)'] [parameters: (u'<domain removed>', 'DE', 'Hohenm\xf6lsen', datetime.datetime(2013, 12, 30, 0, 0), datetime.datetime(2016, 1, 17, 0, 0), '<provider removed>')]

似乎是这个字符串:

'Hohenm\xf6lsen'

为了解决这个问题,我使用了文本工厂

engine = create_engine('sqlite:///myDB.db')
engine.connect().connection.text_factory = str

我也尝试在while循环中使用utf-8从字典列表中读取:

with codecs.open(dictfile, 'rb', encoding='utf-8') as f:

我还检查了所有条目的变量类型,它们都是字符串,而不是数组或blob。

我的完整代码可以在这里看到:

https://github.com/tripledown/domainfinder/blob/master/domainfinder.py

1 个答案:

答案 0 :(得分:0)

添加此行以分配text_factory。它将字符串转换为unicode对象。

conn = GetSqliteConnection(db_path) conn.text_factory = lambda x: unicode(x, 'utf-8', 'ignore')