加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

python sqlite3 中文乱码

(2016-10-22 16:40:00)
标签:

html

javascript

js

python

selenium

分类: python

作为初学才,学起python,是挺快,但是也不免常常出些小错。在访问sqlite3的时候,我写了下边的测试代码。

文件test.py是utf-8编码的,OK,结果没问题。

http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" /> # !/usr/bin/env python 
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />#
 coding=utf-8 
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 import  sqlite3
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 # con=sqlite3.connect(r"sqlite.db") 
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 con = sqlite3.connect(r " e: estsqlite3.db " )
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 # con.text_factory=str 
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 cur  =  con.cursor()
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 # cur.execute("insert into basvslvoy (vslcode,voyage,vslename) values(?,?,?)",(u'tt', u'tt', u'tt')) 
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 cur.execute( " insert into t1 values(?, ?) " ( 10 u ' 中文测试gbk10 '  ))
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />con.commit()
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />cur.execute(u
 " select from t1 where col2 like '中%' " )
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 for  row  in  cur.fetchall():
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />    
 print  row[0], row[ 1 ].encode( ' gbk ' )
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />con.close()
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />

结果如下:

 

http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" /> Process started  >>> 
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 10  中文测试gbk10
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 10  中文测试gbk10
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 5  
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 10  中文测试gbk10
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 10  中文测试gbk10
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 10  中文测试gbk10
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 10  中文测试gbk10
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 10  中文测试gbk10
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 10  中文测试gbk10
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 10  中文测试gbk10
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 10  中文测试gbk10 <<<</span>  Process finished.

可是,当我把文件编码调整成ansi格式的时候,上边程序没办法跑。
结果将文件内容改成下边的样子: 

# !/usr/bin/env python 
#
 coding=utf-8 

import  sqlite3

# con=sqlite3.connect(r"sqlite.db") 
con = sqlite3.connect(r " e: estsqlite3.db " )
# con.text_factory=str 
cur  =  con.cursor()
# cur.execute("insert into basvslvoy (vslcode,voyage,vslename) values(?,?,?)",(u'tt', u'tt', u'tt')) 
cur.execute( " insert into t1 values(?, ?) " ( 10  ' 中文测试gbk10 ' .decode( ' gbk ' ))
con.commit()
cur.execute(
 " select from t1 where col2 like ' "   +   '  ' .decode( ' gbk ' ) + " %' " )
for  row  in  cur.fetchall():
    
 print  row[0], row[ 1 ].encode( ' gbk ' )


con.close()

这样,出现的结果跟上边一样。
问题的关键就是#coding=utf-8,同时文件编码也要是utf-8,才能让结果保持一致。
看看下边的简单例子就知道:

http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" /> >>>   =   ' 中文 ' 
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 >>>   =  u ' 中文 ' 
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 >>>  a
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 ' ÖÐÎÄ ' 
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 >>>  b
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />u
 ' 中文 ' 
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 >>>  a.decode( ' gbk ' )
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />u
 ' 中文 ' 
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />
 >>>   ==  a.decode( ' gbk ' )
http://static.oschina.net/uploads/img/201204/12125334_LTcl.gifsqlite3 中文乱码" />True

如果将上边的例子放到一个.py文件当中,分别采用utf-8编码和ansi编码('cp936'),结果可能就是分别为True和False。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有