2014-09-27 74 views
3

我知道存在類似的問題,但答案不同而且有點令人困惑。Python將UTF8字符串插入到SQLite中

我有這個字符串:

titulo = "Así Habló Zaratustra (Cómic)" 

當我嘗試將其插入到SQLite數據庫我得到的錯誤:

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. 

我試了幾件事情沒有成功。請幫忙。

回答

5

做它告訴你這樣做並使用unicode值改爲:

titulo_unicode = titulo.decode('utf8') 

sqlite3庫將在插入時正確編碼此,選擇時再進行解碼的照顧。