Python 内存中的SQLite数据库未被销毁?

Python 内存中的SQLite数据库未被销毁?,python,python-2.7,sqlite,Python,Python 2.7,Sqlite,我编写了这个非常简单的测试脚本来检查内存中共享缓存数据库的功能,并且在脚本退出后,数据库似乎没有被清理 import sqlite3 def hello(): db = sqlite3.connect('file::memory:?cache=shared') db.execute("CREATE TABLE test (hello varchar(32))") db.execute("INSERT INTO test (hello) VALUES ('world')"

我编写了这个非常简单的测试脚本来检查内存中共享缓存数据库的功能,并且在脚本退出后,数据库似乎没有被清理

import sqlite3

def hello():
    db = sqlite3.connect('file::memory:?cache=shared')
    db.execute("CREATE TABLE test (hello varchar(32))")
    db.execute("INSERT INTO test (hello) VALUES ('world')")
    db.commit()
    c1 = db.execute("SELECT * FROM test")
    print c1.fetchone()
    db.close()

def world():
    wdb = sqlite3.connect('file::memory:?cache=shared')
    c = wdb.execute("SELECT * FROM test")
    print c.fetchone()
    wdb.close()


hello()
world()
我第一次运行这个脚本时,它工作正常,但我忘记了关闭调用。第二次运行时,我得到一个异常,表示该表已经存在。显然,我知道我可以将
如果不存在
添加到
CREATE TABLE
语句中,以消除这种情况,但这种行为似乎暗示数据库仍在内存中。我的解释正确吗?如果是这样,有没有办法把它清理干净


我尝试过对查询进行注释,并简单地打开/关闭连接,但似乎没有任何效果。

Python 2.7中的Python sqlite3模块不支持URI文件名
db=sqlite3.connect('file::memory:?cache=shared')
实际上在磁盘上创建了一个名为“file::memory:?cache=shared”的文件,并且每次都连接到该文件

相反,只需执行
db=sqlite3.connect(':memory:')
,尽管您不会获得共享缓存行为


这种行为,但仅在Python3.4中存在,您需要传递
uri=True
kwarg。

Python2.7中的PythonSQLite3模块不支持uri文件名
db=sqlite3.connect('file::memory:?cache=shared')
实际上在磁盘上创建了一个名为“file::memory:?cache=shared”的文件,并且每次都连接到该文件

相反,只需执行
db=sqlite3.connect(':memory:')
,尽管您不会获得共享缓存行为

这是一种行为,但仅在Python3.4中,您需要传递
uri=True
kwarg