vendredi 7 août 2015

SQLite in-memory database not being destroyed?

I wrote this extremely simple test script to examine the functionality of in-memory shared cache databases, and it seems that the database is not being cleaned up after the script exits.

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()

The first time I ran this script, it worked without issue, but I had forgotten the close calls. The second time I ran it, I got an exception saying that the table already exists. I know, obviously, that I can add IF NOT EXISTS to the CREATE TABLE statement to make this go away, but this behavior would seem to imply that the database is still in memory somehow. Am I interpreting this correctly? If so, is there any way to get it cleaned up?

I've tried commenting the queries and simply opening/closing the connections, but that didn't seem to have any effect.

Aucun commentaire:

Enregistrer un commentaire