2012-02-14 68 views
1

,当我使用sqlite3的数据库SQLAlchemy的图书馆,我得到这个错误SQLite数据库非ASCII字符错误

sqlalchemy.exc.ProgrammingError: (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. 
u'INSERT INTO model_pair (user, password) VALUES (?, ?)' ('\xebE\xc2\xe4.\[email protected]', '123456') 

,这里是一些测试数据:

呆呆 [email protected]  11111                
    è?“言 [email protected]  11111 
[email protected]?€?  11111 

我已经将数据库编码配置为utf-8或gbk,但是在插入时,我既没有成功,也没有成功 ,我尝试str.decode('gbk'),它将卡住像char一样的字符并得到像上面那样的错误。

有人告诉我如何解决这个错误?

+0

对于遇到同样问题的人,请检查我的答案http://stackoverflow.com/questions/23876342/sqlalchemy-programmingerror-can-interpret-8-bit-bytestrings/38513385#38513385 – cdagli 2016-07-21 20:04:32

回答

3

试图改变'\xebE\xc2\xe4.\[email protected]'u'\xebE\xc2\xe4.\[email protected]'

也,你可以尝试做'\xebE\xc2\xe4.\[email protected]'.decode("utf-8"),但它给出了一个错误“无效的延续字节”,也许你的字符串不是合法的UTF-8之后呢?

顺便说一句,是否提到你是运行Python 2.x或3.x,有一个区别。

+0

我使用python2.7和我试图插入sql语句,而不是使用ORM模型导入数据。害怕科学造成麻烦 – CodeFarmer 2012-02-14 10:32:00

+1

你尝试过'u'\ x ...'或'u'\ N {EURO SIGN}“吗? – 2012-02-15 11:31:10