Re: Copy data from tmp db to real db; Easy way?
"Manuel Lanctot" <[email protected]>
| Newsgroups | gmane.comp.python.db.pysqlite.user |
|---|---|
| Message-ID | <[email protected]> |
On 11/13/06, TiNo wrote:
> Hi,
>
> I need to copy data from my temporary database to the definite db. I
> can do this by selecting all data, create new insert statements and
> execute those. But I have a feeling that their is an easier way.
> Something like dumping, buth without the table building statements.
> What is the easy way to do this?
Here's a code snippet written by Gerhard Häring that does what I think
you want.
At least it's perfect for my "Save game/Load game" method.
cx = sqlite.connect(":memory:", isolation_level=None)
cu = cx.cursor()
def saveDisk(cx, filename):
cur = cx.cursor()
cur.execute("attach '%s' as savefile" % filename)
cur.execute("select name from sqlite_master where type='table'")
table_names = cur.fetchall()
for table_name, in table_names:
cur.execute("create table savefile.%s as select * from %s" %
(table_name, table_name))
cur.execute("detach savefile")
# This will dump the in-memory database to a file, which you can then connect to
# with a new connect(). The same could be done to copy a DB to another one or
# load a file DB into :memory:.
Cheers,
Manuel Lanctôt