1

What I need to do is load an sqlite3 database from the disk into memory, work with it, and when the script exits save the in-memory database to the disk. How would I go about doing this? Thanks!

1 Answers1

1

All you need to do is connect to the database - you can do anything you want to with it then.

from sqlite3 import connect

conn = connect("/path/to/your/sqlite.db")
# Do what you need to with the database here
# Changes (inserts, updates, etc) will be persisted
# to disk whenever you commit a transaction.

If you need to be able to run a series of commands and roll them all back if one does not succeed you should use a transaction.

If you need to make a copy of the existing database, make alterations to it and then save it somewhere else only if the alterations succeed then you can do one of the following things:

  • Create an in-memory database and then use SQLite's ATTATCH DATABASE command to attach the existing database to the new in-memory database. Once you have copied all of the data over from the existing database into the in-memory database and transformed it you can attach a new database and copy everything over again.
  • Copy the existing database file, attempt to make alterations to it, and delete the new file if the operations did not succeed.
  • Use sqlite3's Connection.iterdump to get the contents of the tables as a SQL script which you can then pass to sqlite3.Cursor.executescript as this answer suggests (using either an in-memory or an on-disk connection string for your new database).
Community
  • 1
  • 1
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • 1
    But whenever I query it, it'll query the file on the disk. I want this to run in memory (I've heard it's faster). –  Mar 18 '12 at 15:43
  • @Fike - unless you *know* that disk-IO is (or will be) your bottleneck you probably don't need to make this optimization - but I gave you some suggestions :-) – Sean Vieira Mar 18 '12 at 15:52
  • 1
    @Sean Vieira While I agree with your sentiment, you didn't technically answer his question. For those of you looking for a real answer, this is one way to do it, though I haven't tested it out yet: http://stackoverflow.com/a/10856450/353094 – leetNightshade Nov 07 '12 at 22:52