Implementation of SQLite3 Chinese code Python

  • 2020-05-19 05:07:19
  • OfStack

More than 100,000 texts were read and written to the SQLite type database. Because there were Chinese characters in the text, it was correct to insert them into the database. When the text was taken out, 1 was UnicodeDecodeError, which led to a day of trouble.

The final solution:

When connecting to data, Python is set as follows:

db=sqlite3.connection("...")

db.text_factory=st


In addition, the hard-coded Chinese strings in the python code do not have problems, except to add them at the beginning of the source code

# -*- coding:utf-8 -*-

Set the python source code to utf-8

import sys

reload(sys)

sys.setdefaultencode('utf8')


Related articles: