Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python临时文件的Sqlite 2 python_Python_Sqlite_Temporary Files_Pysqlite - Fatal编程技术网

使用python临时文件的Sqlite 2 python

使用python临时文件的Sqlite 2 python,python,sqlite,temporary-files,pysqlite,Python,Sqlite,Temporary Files,Pysqlite,使用存储为python临时文件的数据库时,我在sqlite中遇到了一些奇怪的行为。基本上,查询sqlite_主表工作正常,但查询另一个表会返回: DatabaseError: database disk image is malformed 源数据库存储为python临时文件: ntf=tempfile.NamedTemporaryFile(suffix='.gdb') 下面是一个查询函数 def sqliteQ(string,filename): '''

使用存储为python临时文件的数据库时,我在sqlite中遇到了一些奇怪的行为。基本上,查询sqlite_主表工作正常,但查询另一个表会返回:

DatabaseError: database disk image is malformed
源数据库存储为python临时文件:

ntf=tempfile.NamedTemporaryFile(suffix='.gdb')
下面是一个查询函数

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的转换管道有问题,但奇怪的是,第一个查询有效,第二个查询无效。

您要导入什么sqlite模块?我在Debian上,在[66]:sqlite.version Out[66]中安装了包
python sqlite
:“1.0.1”和sqlite客户端的版本是什么(启动时打印)?
sqlite版本2.8.17
我使用的文件是.gdb(sqlite2)您导入的sqlite模块是什么?我在Debian上,已经在[66]:sqlite.version Out[66]中安装了包
python sqlite
:“1.0.1”以及sqlite客户端的版本是什么(启动时打印)?
sqlite版本2.8.17
我使用的文件是.gdb(sqlite2)