2

I wolud like to copy sqlite db from memory to hard drive. How can I do?

I try this way:

conn_phy = sqlite3.connect("phy.db")
conn = sqlite3.connect(":memory:")
c = conn.cursor()
c_phy = conn.cursor()

for row in c.execute("select * from table"):
    c_phy.execute(' insert into tablephy (column1, column2, column3) values\
     (?,?,?)', row) #phy db column number equal memory column number
    conn_phy.commit()
Ermi
  • 69
  • 5
  • You can dump it. More here: [how do i dump a single sqlite3 table in python?](http://stackoverflow.com/questions/6677540/how-do-i-dump-a-single-sqlite3-table-in-python) – Dmitry Nedbaylo Jan 27 '15 at 10:21
  • It seems there is a typo, you might mean: `c_phy = conn_phy.cursor()` instead. Have you tried to pass `conn_phy` instead of `conn` to the code that populates `conn`? – jfs Jan 27 '15 at 11:42

1 Answers1

4

sqlite3 Python module does not expose sqlite3_backup_*() C functions. There is probably no direct way to save in-memory database into a database file on disk.

An analog of old_db.iterdump() + new_db.executescript() could be used (not tested):

import sqlite3

old_db = sqlite3.connect(':memory:')
# here's code that works with old_db
# ...

# dump old database in the new one
with sqlite3.connect('test.db') as new_db:
    new_db.executescript("".join(old_db.iterdump()))

Based on @mouad's answer to the opposite quesiton (loading from file to in-memory db).

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670