Hi, forced to eating my dogfood after some time and found i need
something better, here is the better version -- looks complicated, but
i cant see better..

class CommonDatabase(object):
    def duplicateToDisk(self, target_file):
        connection = self.engine.connect() #this is in my instance
        original_meta = self.metadata #as well
        new_meta = MetaData()

        if os.path.exists(target_file):
            os.remove(target_file)
        try:
            connection.execute("attach '%s' as extern" % target_file)
            #make a copy of metadata
            for table in original_meta.tables.values():
                table.tometadata(new_meta)
            #set the scheme, important - and delete indexes (is there
a better way?)
            for table in new_meta.tables.values():
                table.schema = 'extern'
                del(table.indexes)
                table.create(connection)
            #copy content
            for table in original_meta.tables.values():
                connection.execute("insert into extern.%s select *
from %s" % (table, table))
            connection.execute("detach extern")

            #connect to it and recreate indexes
            engine = create_engine('sqlite:///%s' % target_file,
echo=True)
            for t in original_meta.tables.values():
                for ix in t.indexes:
                    ix.create(bind=engine)

        except Exception, e:
            log.error("Exception duplicating database to file: %s" %
target_file)
            log.traceback()


On Oct 20, 8:24 pm, rca <[email protected]> wrote:
> Hmm :)
>
> I am looking for the same thing, I used to do it with sql, but I hoped
> to make it more orm
> Here is how i do it for sqlite
>
>   defduplicateToDisk(self, file):
>     '''Tohle ulozi databazi, ktera byla pouze v pameti, na disk'''
>     cur = self.connection()
>     import os
>     if os.path.exists(file):
>       os.remove(file)
>     cur.execute("attach %s as extern" % file)
>
>     self.checkTable('extern.dictionary')
>     cur.execute("insert into extern.dictionary select * from
> dictionary")
>     cur.execute("detach extern")
>     self.commit()
>
> On Sep 17, 8:51 pm, "Doug Farrell" <[email protected]> wrote:
>
> > Hi all,
>
> > I'm using SqlAlchemy with Sqlite to maintain a dynamic state table for a
> > constantly running process. The table for this is kept in memory using
> > the create_engine('sqlite:///:memory:') function call. My process is an
> > endlessly running loop (with a sleep state), and I'd like to read the
> > databse in from a disk file into the memory structure, process through
> > my loop, and then write the state back out to the disk file, completely
> > replacing the contents of the disk copy. Can anyone tell me, help me,
> > advise me how I might go about doing this? I'm new to SqlAlchemy (noob),
> > so forgive me if this is an dopey question.
>
> > Thanks in advance,
> > Doug
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to