2012-04-17 14 views
2

pythonでsqlite3データベースに格納されたutf-8エンコード値の抽出に問題があります。Pythonのsqlite3からのUnicode値のクエリと抽出

>> import sqlite3 
>> connection=sqlite3.connect('mySQLite3DB.db') 
>> cursor=connection.cursor() 
>> word = unichr(2675)+unichr(37) # ੳ% 
>> cursor.execute('select distinct col1 from table1 where col1 like ? limit 3', word) 
--------------------------------------------------------------------------- 
ProgrammingError       Traceback (most recent call last) 
/Users/punjcoder/code/<ipython-input-10-358f7ffe8df0> in <module>() 
----> 1 cursor.execute('select distinct col1 from table1 where col1 like ? limit 3', word) 

ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied. 

ここで、手動でユニコードを挿入してクエリを実行すると、それが実行されます。しかし、私はテキストを取得することはできませんが、代わりにptrをバッファリングします。

>> cursor.execute('select distinct col1 from table1 where col1 like "ੳ%" limit 3') 
<sqlite3.Cursor at 0x10dab8500> 
>> for row in cursor.fetchall(): 
>>  print row 
(<read-write buffer ptr 0x10dabf898, size 3 at 0x10dabf858>,) 

私はすでに以下のリンクを見てきましたが、動作させる方法が見つからないようです。私はPython 2.7.2とSQLite 3.7.10に取り組んでいます。あなたの助けは前もって評価されています。 Extracting values from SQLite database in Python

答えて

3

試してみてください。

cursor.execute('select distinct col1 from table1 where col1 like ? limit 3', [word]) 

私はあなたが反復可能としてUnicode文字列wordを処理し、個別に各文字を見ていることを期待しています。

+0

これは、不適切な数のバインディングを助けました。 2番目は、スクリプトの先頭にこれを追加すると助けになりました。 import sys reload(sys) sys.setdefaultencoding( "utf-8") – PunjCoder

+1

@PunjCoder、その "トリック"は使用しないでください。 'ascii'のデフォルトのエンコーディングは、いくつかの標準ライブラリによって期待されています。代わりに明示的に.decode( 'utf8') '代わりに。 –

関連する問題