Realize the method of returning and querying Chinese field in SQLite Python
- 2021-07-18 08:41:26
- OfStack
The blogger has been stuck on this issue for a long time, and posted a solution to help friends who need it, and directly put it on the code (test environment: win10+Python2.7):
# coding=utf-8
import sqlite3
with sqlite3.connect(":memory:") as conn:
try:
init_sql = " create table test (id integer primary key ,name text(200) not null);" \
" insert into test (name) values (' Small residence ');" \
" insert into test (name) values (' Big residence ');"
conn.executescript(init_sql)
except Exception as e:
conn.rollback()
raise e
else:
conn.commit()
conn.text_factory = str # Here is the key ,sqlite The default text access is Unicode
try:
for row in conn.execute(" select * from test where name = ?",(" Big residence ",)):
print row[1],type(row[1])
except Exception as e:
raise e
Results:
Big residence <type 'str'>