我在使用存储为python tempfile的数据库时遇到了一些奇怪的行为。基本上,查询sqlite_主表可以正常工作,但是查询另一个表会返回:
DatabaseError: database disk image is malformed
源数据库存储为python tempfile:
^{pr2}$下面是一个查询函数。在
def sqliteQ(string,filename):
'''
string= query string
filename= string file name
'''
con=sqlite.connect(filename)
c= con.cursor()
c.execute(string)
out=c.fetchall()
c.close()
return out
我可以像这样成功地查询数据库:
sq=db.sqliteQ("select sql from sqlite_master where type='table' and name='managements'", gdb.name)
In [13]: sq
Out[13]: 'CREATE TABLE managements (path varchar(255), name varchar(255), data varchar(50000), date date, owner varchar(64), password varchar(64), prot text)'
但是,以下命令将返回错误:
In[14]: m=db.sqliteQ('select * from managements', gdb.name)
41 con=sqlite.connect(filename)
42 c= con.cursor()
---> 43 c.execute(string)
44 out=c.fetchall()
45 c.close()
/usr/lib/python2.7/dist-packages/sqlite/main.pyc in execute(self, SQL, *parms)
242 if len(parms) == 0:
243 # If there are no paramters, just execute the query.
--> 244 self.rs = self.con.db.execute(SQL)
245 else:
246 if len(parms) == 1 and \
DatabaseError: database disk image is malformed
我可以使用sqlite命令行工具成功地运行这两个查询。任何建议都会很有帮助。在
彼得
更新
似乎只有在使用tempfile
时才会遇到这个问题。我正在下载一个.zip文件,并将随附的sqlite数据库读取到cStringIO
,并使用以下函数写入tempfile
:
def tGDB (URLo):
mem=io.StringIO(URLo.read())
zf=zp.ZipFile(mem)
ntf=tempfile.NamedTemporaryFile(suffix='.gdb')
ntf.write(zf.read(zf.namelist()[0]))
return ntf
我想这可能表明从zip到tempfile的转换管道有问题,但是奇怪的是第一个查询有效,第二个查询不起作用
目前没有回答
相关问题 更多 >
编程相关推荐