''' https://stackoverflow.com/questions/3850022/how-to-load-existing-db-file-to-memory-in-python-sqlite3 The followinng python program reads an existing sqlite3 database from disk to memory. It first reads the entire table by select *, then executes the sequence of sqlite3 instructions to create an in-memory database. Note: The memory version of a sqlite db can't simply do queries such as cur.execute("select * from stocks") it must be done using replacement query such as cur.execute("select * from stocks where trans_text=:type", {"type":"BUY"}) Xiannong Meng 2021-10-29 ''' import sqlite3 source = sqlite3.connect('example.db') dest = sqlite3.connect(':memory:') #dest = sqlite3.connect('copy.db') source.backup(dest) cur = dest.cursor() # in the following line, the reference 'type' is replacing the field # 'trans_text' in the table 'stocks' cur.execute("select * from stocks where trans_text=:type", {"type":"BUY"}) #cur.execute("select * from stocks") # can't do this for in-memory db print(cur.fetchall()) # We can also close the connection if we are done with it. # Just be sure any changes have been committed or they will be lost. source.close() dest.close()