How to load existing db file to memory in Python sqlite3? How to load existing db file to memory in Python sqlite3? sqlite sqlite

How to load existing db file to memory in Python sqlite3?


Here is the snippet that I wrote for my flask application:

import sqlite3from io import StringIOdef init_sqlite_db(app):    # Read database to tempfile    con = sqlite3.connect(app.config['SQLITE_DATABASE'])    tempfile = StringIO()    for line in con.iterdump():        tempfile.write('%s\n' % line)    con.close()    tempfile.seek(0)    # Create a database in memory and import from tempfile    app.sqlite = sqlite3.connect(":memory:")    app.sqlite.cursor().executescript(tempfile.read())    app.sqlite.commit()    app.sqlite.row_factory = sqlite3.Row


What about sqlite3.Connection.backup(...)? "This method makes a backup of a SQLite database even while it’s being accessed by other clients, or concurrently by the same connection." Availability: SQLite 3.6.11 or higher. New in version 3.7.

import sqlite3source = sqlite3.connect('existing_db.db')dest = sqlite3.connect(':memory:')source.backup(dest)


sqlite3.Connection.iterdump "[r]eturns an iterator to dump the database in an SQL text format. Useful when saving an in-memory database for later restoration. This function provides the same capabilities as the .dump command in the sqlite3 shell."

Get such an iterator and dump the disk-based database into a memory-based one, and you're ready to compute. When the computation is done, just dump the other way around back to disk.