SQLite-Python中如何返回、查询中文字段
2018-02-28 13:43
423 查看
博主在这个问题上卡了挺久的,贴出来解决方法帮助需要的朋友,直接上代码(测试环境: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 ('小居居');" \
" insert into test (name) values ('大居居');"
conn.executescript(init_sql)
except Exception as e:
conn.rollback()
raise e
else:
conn.commit()
conn.text_factory = str # 此处是关键,sqlite默认文本存取是Unicode
try:
for row in conn.execute(" select * from test where name = ?",("大居居",)):
print row[1],type(row[1])
except Exception as e:
raise e结果:大居居 <type 'str'>
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 ('小居居');" \
" insert into test (name) values ('大居居');"
conn.executescript(init_sql)
except Exception as e:
conn.rollback()
raise e
else:
conn.commit()
conn.text_factory = str # 此处是关键,sqlite默认文本存取是Unicode
try:
for row in conn.execute(" select * from test where name = ?",("大居居",)):
print row[1],type(row[1])
except Exception as e:
raise e结果:大居居 <type 'str'>
相关文章推荐
- 如何对37,65535的字段进行中文模糊查询
- C#与Firebird开发-查询返回别名中文字段
- 在arcgis使用python脚本进行字段计算时是如何解决中文问题的
- 查询数据库时如何把数据按照字段名和字段值的数据返回
- Python实现Sqlite将字段当做索引进行查询的方法
- Python Sqlite3以字典形式返回查询结果的实现方法
- Hibernate 查询多个字段返回的list如何转为自定义对象
- python sqlite 查询表的字段名 列名
- Hibernate 查询多个字段返回的list如何转为自定义对象,以及转换异常
- Python查询Mysql, sqlite时返回字典结构的代码
- Android中SQLite查询date类型字段出现有返回但是为错误值的情况
- 在arcgis使用python脚本进行字段计算时是如何解决中文问题的
- 在arcgis使用python脚本进行字段计算时是如何解决中文问题的
- python如何向sqlite3中插入中文字符串
- Python Sqlite3以字典形式返回查询结果的实现方法
- SQL Server 查询中文字段返回为空
- 记录:java执行mysql语句查询字段类型:timestamp返回页面显示会多出个 .0,自己如何处理的
- elasticsearch 在查询的时候如何返回指定的字段值?
- Python查询Mysql, sqlite时返回字典结构的代码
- Python查询Mysql, sqlite时返回字典结构的代码