2012-02-14 14 views
1

私はSQLAlchemyのライブラリとsqlite3のデータベースを使用する場合、私はこのエラーに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')を試してみましたが、上記のようなエラーが表示されます。

誰でもこのエラーを回避する方法を教えてください。

+0

同じ問題が発生した場合は、私の答えを確認してください。http://stackoverflow.com/questions/23876342/sqlalchemy-programmingerror-can-interpret-8-bit-bytestrings/38513385#38513385 – cdagli

答えて

3

'\xebE\xc2\xe4.\[email protected]'

u'\xebE\xc2\xe4.\[email protected]'にを変更しようとすると、おそらくあなたの文字列は、すべての後に有効なUTF-8ではありません、あなたは '\xebE\xc2\xe4.\[email protected]'.decode("utf-8")を行うことを試みることができるが、それはエラー「無効な継続バイト」を与えますか?

btw、あなたはPython 2.xまたは3.xを実行していると言えますが、違いがあります。

+0

私はpython2.7を使用しています。 ORMモデルを使用してデータをインポートするのではなく、SQL文を挿入しようとしています。科学的な原因のトラブルの中で – CodeFarmer

+1

あなたは 'u '\ x ...''または 'u" \ N {EURO SIGN} "'を試しましたか? –