Moving back and forth between an on-disk database and a fast in-memory database?

前端 未结 2 574
傲寒
傲寒 2020-12-09 04:05

Python\'s sqlite3 :memory: option provides speedier queries and updates than the equivalent on-disk database. How can I load a disk-based database in

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-09 05:07

    The answer at How to load existing db file to memory in Python sqlite3? provided the important clues. Building on that answer, here is a simplification and generalization of that code.

    It eliminates eliminate the unnecessary use of StringIO and is packaged into reusable form for both reading into and writing from an in-memory database.

    import sqlite3
    
    def copy_database(source_connection, dest_dbname=':memory:'):
        '''Return a connection to a new copy of an existing database.                        
           Raises an sqlite3.OperationalError if the destination already exists.             
        '''
        script = ''.join(source_connection.iterdump())
        dest_conn = sqlite3.connect(dest_dbname)
        dest_conn.executescript(script)
        return dest_conn
    
    if __name__ == '__main__':
        from contextlib import closing
    
        with closing(sqlite3.connect('pepsearch.db')) as disk_db:
            mem_db = copy_database(disk_db)
    
        mem_db.execute('DELETE FROM documents WHERE uri="pep-3154"')
        mem_db.commit()
    
        copy_database(mem_db, 'changed.db').close()
    

提交回复
热议问题