[Python] 文字コードのエラーについて

app

この記事は2022年3月5日に書かれたものです。情報が古い可能性がありますのでご注意ください。

エラーメッセージ


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.

対処法

pythonのsqlite3は、textをunicodeで返してくる。
データの文字コード(UTF-8)の問題でエラーが出た場合は下記のように「text_factory = str」行を追加することで解決。


~
conn = create_connection(database_path)
conn.text_factory = str
with conn:
~