I'm experiencing some strange behavior from sqlite using a database stored as a python tempfile. Essentially, querying the sqlite_master table works fine, but querying another table returns:
DatabaseError: database disk image is malformed
The source database is stored as a python tempfile:
ntf=tempfile.NamedTemporaryFile(suffix='.gdb')
The following is a query function.
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
I am able to successfully query the database like this:
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)'
However the following returns the error:
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
I can successfully run both queries using the sqlite command line tool. Any suggestions would be most helpful.
Peter
UPDATE
It seems that I only encounter this problem when using the tempfile
. I am downloading a .zip file and reading the enclosed sqlite database to cStringIO
and writing to the tempfile
using this function:
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
I guess that this may indicate there is a problem with the conversion pipeline from zip to tempfile, but its quite strange that the first query works and the second doesn't.