我正在使用来自 python 的 SQLite 数据库(使用SQLAlchemy)。出于性能原因,我想在应用程序中填充一个内存数据库,然后将该数据库备份到磁盘。
SQLite 有一个备份 API,它似乎可以透明地做到这一点。
APSW文档说它包装了备份 API,但我想从 Python 的标准 sqlite3 模块访问此功能,或者最好从 SQLAlchemy 访问此功能。这可能吗?
我正在使用来自 python 的 SQLite 数据库(使用SQLAlchemy)。出于性能原因,我想在应用程序中填充一个内存数据库,然后将该数据库备份到磁盘。
SQLite 有一个备份 API,它似乎可以透明地做到这一点。
APSW文档说它包装了备份 API,但我想从 Python 的标准 sqlite3 模块访问此功能,或者最好从 SQLAlchemy 访问此功能。这可能吗?
APSW 方言也可以很容易地添加到 SQLAlchemy 中。在 0.6 中很容易实现,它允许多种 DBAPI 适配器对正在使用的数据库使用通用方言。
如果 pysqlite 和 apsw 链接到同一个 sqlite 库,那么 pysqlite 可以接受 apsw 连接。看:
http://docs.pysqlite.googlecode.com/hg/sqlite3.html#combining-apsw-and-pysqlite
我将尝试使用这种(和其他)方法来让 apsw 与 SQLAlchemy 一起工作,因为它们是一个非常有用的组合。
It's 2021, performance difference between memory and disk has changed. My gaming notebook can perform SQL operations, mostly INSERTs, 100x faster in memory than disk. I'm using raw connections to backup (copy in memory db to disk).
# create in-memory database. get raw_connection.
engine_memory = sqlalchemy.create_engine('sqlite://')
raw_connection_memory = engine_memory.raw_connection()
# do something to in-memory db
raw_cursor_memory.executescript(my_sql_script)
# save memory db to disk file.
engine_file = sqlalchemy.create_engine('sqlite:///myfile.sqlite')
raw_connection_file = engine_file.raw_connection()
raw_connection_memory.backup(raw_connection_file.connection)
raw_connection_file.close()
engine_file.dispose()
python-sqlite3-backup模块声称可以解决这个问题。