[Tutor] in-memory pysqlite databases

Alan Gauld alan.gauld at btinternet.com
Sat Apr 12 21:20:20 CEST 2008


"Dinesh B Vadhia" <dineshbvadhia at hotmail.com> wrote 

> Say, you have already created a pysqlite database "testDB".  
> In a Python program, you connect to the database as:

> con = sqlite3.connect("testDB")
> cur = con.cursor()

OK so far.

> To use a database in memory (ie. all the 'testDB' tables 
> are held in memory) the pysqlite documentation says 
> the declaration is:

> con = sqlite3.connect(":memory:")
> cur = con.cursor()

No it doesn't. It says to create and use a database in memory 
you use that syntax. It makes no claims about being able to 
use a file based database in memory.

The normal way to prepopulate an in memory database is 
to have a function (possibly in another module) that creates 
the structure and loads the data. That way you can easily 
move the data to a file based solution if needed.

> But, this can't be right as you're not telling 
> Python/pysqlite which database to keep in memory.  

You are creating a new database in memory.

What would be nice is a function that could persist an 
in-memory database to disk. But I';m not aware of any 
such feature.

HTH,

Alan G



More information about the Tutor mailing list