Re: Convert readonly file to :memory: database

Gerhard Häring <[email protected]>
Newsgroups gmane.comp.python.db.pysqlite.user
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thomas Heller wrote:
> I have a sqlite database in a readonly file.
> Is it possible to create a :memory: database and quickly initialize it
> from the file?

That's possible using a combination of ATTACH/DETACH commands and
schema introspection. I've attached a draft.

- -- Gerhard
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHYm6XdIO4ozGCH14RAozbAJ0X2rgmxVPXYNO53I1dZAv0vAg0WwCZATQx
RUOVVzJfGrQRlqBGDIyqsG4=
=rW27
-----END PGP SIGNATURE-----

_______________________________________________
pysqlite mailing list
pysqlite-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/pysqlite
copydb.py (text/x-python, 849 B)
from pysqlite2 import dbapi2 as sqlite

def copy_database(src_db_name, dest_con):
    """
    Copies contentents from one database to another.

    src_db_name: path to source database
    dest_con:    destination pysqlite connection
    """
    # TODO create indices like in source databaes
    try:
        src_con = sqlite.connect(src_db_name)
        dest_con.execute("attach '%s' as srcdb" % src_db_name)
        for row in src_con.execute("select name from sqlite_master where type = 'table'"):
            dest_con.execute("create table %s as select * from srcdb.%s" % (row[0], row[0]))
        dest_con.execute("detach srcdb")
    finally:
        src_con.close()

if __name__ == "__main__":
    dest = sqlite.connect(":memory:")
    copy_database("/tmp/trac.db", dest)
    # print dest.execute("select count(*) from ticket").fetchone()[0]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.