2010年12月19日日曜日

pythonのSQLiteでUTF-8を使う

pythonのsqlite3は標準でTEXTがunicodeを要求するようです。何も考えずにUTF-8を投げたらエラーを食らいました。

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.

SQLiteでUTF-8を用いたい場合はtext_factoryにstrを指定して解決しました。
import sqlite3
db = sqlite3.connect(":memory:")
db.text_factory = str

0 件のコメント:

コメントを投稿