经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » SQLite » 查看文章
在SQLite-Python中实现返回、查询中文字段的方法
来源:jb51  时间:2019/7/17 13:12:32  对本文有异议

博主在这个问题上卡了挺久的,贴出来解决方法帮助需要的朋友,直接上代码(测试环境:win10+Python2.7):

  1. # coding=utf-8
  2. import sqlite3
  3. with sqlite3.connect(":memory:") as conn:
  4. try:
  5. init_sql = " create table test (id integer primary key ,name text(200) not null);" " insert into test (name) values ('小居居');" " insert into test (name) values ('大居居');"
  6. conn.executescript(init_sql)
  7. except Exception as e:
  8. conn.rollback()
  9. raise e
  10. else:
  11. conn.commit()
  12. conn.text_factory = str # 此处是关键,sqlite默认文本存取是Unicode
  13. try:
  14. for row in conn.execute(" select * from test where name = ?",("大居居",)):
  15. print row[1],type(row[1])
  16. except Exception as e:
  17. raise e

结果:

  1. 大居居 <type 'str'>

以上这篇在SQLite-Python中实现返回、查询中文字段的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号