【本期导读】使用PySQLite连接SQLite遇到的问题与解决方法
Python要操作SQLite可以使用Pyslqite模块,最新的模块可以去
www.pysqlite.org下载,当初下载的时候顺便简单看了看usage-guide(
http://initd.org/pub/software/pysqlite/doc/usage-guide.html),觉得挺简单的,没有仔细研究便开始考虑编写,我考虑把sqlite的访问写成一个类,在类的初始化和销毁时候分别建立连接和关闭连接,然后再添加一个执行sql语句的方法,由于传入的sql可能没有返回值也可能有返回值,而返回的值可能是单列值也可能是多条记录,基于这种复杂情况,就让这个方法返回一个列表,管它返回什么,通通加到列表中然后返回,我觉得在这一点上,python比其它语言方便多了。下面是这个类:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# $Id: dbconnect.py 8 2006-04-08 14:21:32Z Administrator $
import ConfigParser
import locale
from pysqlite2 import dbapi2 as sqlite

class SqliteConnection:
def __init__(self,dbname):
"""
初始化数据库连接
"""
self.conn = sqlite.connect(dbname)
def execute(self,sql):
"""
执行传入的sql语句,返回一个元组或者None
"""
self.cu=self.conn.cursor()
self.cu.execute(sql)
self.conn.commit()

self.rows=[]
for self.row in self.cu:
self.rows.append(self.row)
return self.rows
def __del__(self):
"""关闭数据库连接"""
self.conn.close()
def test():
"""测试方法"""
config=ConfigParser.ConfigParser()
config.read('config')
dbname=config.get('SQLiteDB','filename')
test= SqliteConnection(":memory:")
test.execute("create table person(lastname, firstname)")
test.execute("insert into person(lastname,firstname) values('三','张')")
test.execute("insert into person(lastname,firstname) values('四','李')")
if __name__=='__main__':
test()之前简单试过一下访问sqlite一切OK,可是当然运行一下这个类时,结果居然出错了:
Traceback (most recent call last):
File "D:\source\dbconnect.py", line 49, in
?
test()
File "D:\source\dbconnect.py", line 42, in
test
temp=test.execute("select * from person")
File "D:\source\dbconnect.py", line 22, in
execute
self.cu.execute(sql)
pysqlite2.dbapi2.OperationalError: Could not decode to UTF-8 column firstname wi
th text 张
既然出错了,那么大概是这几种情况:
1.pysqlite不支持中文
2.sqlite不支持中文
3.代码出了问题
出错之后,经过测试,排除第1和2两种可能,
于是我仔细简单代码,可还是没有找到出错原因,于是想查看pysqlite源代码,可是他的代码是封闭在一个pyd的二进制文件中的,没办法查看,于是去下载pysqlite的源代码,拿到源代码后在pysqlite-2.2.0\src\cursor.c这个代码中找到了OperationalError错误类型,知道原来是字符转换时出这个错。于是我就试试将返回值转为可是我弄了很久都没有弄好,在网上google一下也没有找到这方面的资料,找了一天了也没有找到解决的办法,无奈之下险些泄气。虽然之前看过pysqlite文档,但看得不仔细,只是简单浏览,当时只是想这东西应该挺简单的,例子也够详细就没有花太多时间去看文档,为得也是节省时间。现在遇到问题了,还是老老实实仔细看看pysqlite文档吧。果然不枉我滴着眼药水把它看完,终于找到原因了,原来pysqlite中有个
con.text_factory可以解决这个问题,这个参数默认值是unicode ,现在只需要把它设置成str就可以了:


1
#!/usr/bin/python
2
# -*- coding: UTF-8 -*-
3
# $Id: dbconnect.py 8 2006-04-08 14:21:32Z Administrator $
4
import ConfigParser
5
import locale
6
from pysqlite2 import dbapi2 as sqlite
7
8
class SqliteConnection:
9
10
def __init__(self,dbname):
11
"""
12
初始化数据库连接
13
"""
14
self.conn = sqlite.connect(dbname)
15
self.conn.row_factory = sqlite.Row #加上这句才能使用列名来返回值
16
self.conn.text_factory=str #加上这一句,否则出现"Could not decode to UTF-8 column"错误
17
def execute(self,sql):
18
"""
19
执行传入的sql语句,返回一个元组或者None
20
"""
21
self.cu=self.conn.cursor()
22
self.cu.execute(sql)
23
self.conn.commit()
24
25
self.rows=[]
26
for self.row in self.cu:
27
self.rows.append(self.row)
28
return self.rows
29
def __del__(self):
30
"""关闭数据库连接"""
31
self.conn.close()
32
def test():
33
"""测试方法"""
34
config=ConfigParser.ConfigParser()
35
config.read('config')
36
dbname=config.get('SQLiteDB','filename')
37
38
test= SqliteConnection(":memory:")
39
test.execute("create table person(lastname, firstname)")
40
test.execute("insert into person(lastname,firstname) values('三','张')")
41
test.execute("insert into person(lastname,firstname) values('四','李')")
42
temp=test.execute("select * from person")
43
print temp
44
encoding = locale.getdefaultlocale()[1]
45
print str('第一条记录:').decode(encoding)+str(temp[0][1]).decode(encoding)+str(temp[0][0]).decode(encoding)
46
print str('第二条记录:').decode(encoding)+str(temp[1][1]).decode(encoding)+str(temp[1][0]).decode(encoding)
47
assert str(temp[0][1]).decode(encoding)+str(temp[0][0]).decode(encoding)==str("张三").decode(encoding)
48
if __name__=='__main__':
49
test()至此总算解决了这个困扰我一天的问题了。
【下期提示】开始学习wxPython,体验一下它的强大界面表现力。