6

我正在使用来自 python 的 SQLite 数据库(使用SQLAlchemy)。出于性能原因,我想在应用程序中填充一个内存数据库,然后将该数据库备份到磁盘。

SQLite 有一个备份 API,它似乎可以透明地做到这一点。

APSW文档说它包装了备份 API,但我想从 Python 的标准 sqlite3 模块访问此功能,或者最好从 SQLAlchemy 访问此功能。这可能吗?

4

5 回答 5

3

APSW 方言也可以很容易地添加到 SQLAlchemy 中。在 0.6 中很容易实现,它允许多种 DBAPI 适配器对正在使用的数据库使用通用方言。

于 2009-07-03T05:54:32.853 回答
3

如果 pysqlite 和 apsw 链接到同一个 sqlite 库,那么 pysqlite 可以接受 apsw 连接。看:

http://docs.pysqlite.googlecode.com/hg/sqlite3.html#combining-apsw-and-pysqlite

我将尝试使用这种(和其他)方法来让 apsw 与 SQLAlchemy 一起工作,因为它们是一个非常有用的组合。

于 2010-08-06T17:21:03.167 回答
2

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()
于 2021-04-19T12:37:00.320 回答
1

当我执行以下操作时

import sqlite3
dir(sqlite3)

我没有看到任何备份 API方法。

因此,答案是否定的;您无法从 sqlite3 模块访问 API。似乎没有人实施它。

于 2009-07-02T10:18:07.330 回答
1

python-sqlite3-backup模块声称可以解决这个问题。

于 2013-08-12T14:16:09.367 回答