在SQLite-Python中实现返回、查询中文字段的方法

yipeiwu_com6年前Python基础

博主在这个问题上卡了挺久的,贴出来解决方法帮助需要的朋友,直接上代码(测试环境: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'>

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

相关文章

python getpass模块用法及实例详解

python getpass模块用法及实例详解

getpass import getpass username = input("username:") password = getpass.getpass("passwor...

Mac在python3环境下安装virtualwrapper遇到的问题及解决方法

前言 我在使用mac安装virtualwrapper的时候遇到了问题,搞了好长时间,才弄好,在这里总结一下分享出来,供遇到相同的问题的朋友使用,少走些弯路。 问题说明: Mac默认系...

详解Django+Uwsgi+Nginx的生产环境部署

详解Django+Uwsgi+Nginx的生产环境部署

使用runserver可以使我们的django项目很便捷的在本地运行起来,但这只能在局域网内访问,如果在生产环境部署django,就要多考虑一些问题了。比如静态文件处理,安全,效率等等,...

Python通过正则表达式选取callback的方法

本文实例讲述了Python通过正则表达式选取callback的方法。分享给大家供大家参考。具体如下: 最近在瞎想怎么通过xpath去精确抓取文章的正文,跟parselets类似的想法,只...

python实现图片转字符小工具

python实现图片转字符小工具

本文实例为大家分享了python图片转字符小工具的具体实现代码,供大家参考,具体内容如下 from PIL import Image #灰度与字符的映射 ascii_char =...