如何在 Python sqlite3 中将现有的 db 文件加载到内存中?

2021-12-08 00:00:00 python performance sqlite

我有一个现有的 sqlite3 db 文件,我需要对其进行一些广泛的计算.从文件中进行计算非常缓慢,而且由于文件不大(~10 MB),因此将其加载到内存中应该没有问题.

I have an existing sqlite3 db file, on which I need to make some extensive calculations. Doing the calculations from the file is painfully slow, and as the file is not large (~10 MB), so there should be no problem to load it into memory.

是否有一种 Pythonic 的方法可以将现有文件加载到内存中以加快计算速度?

Is there a Pythonic way to load the existing file into memory in order to speed up the calculations?

推荐答案

这是我为我的 Flask 应用程序编写的代码片段:

Here is the snippet that I wrote for my flask application:

import sqlite3
from io import StringIO

def init_sqlite_db(app):
    # Read database to tempfile
    con = sqlite3.connect(app.config['SQLITE_DATABASE'])
    tempfile = StringIO()
    for line in con.iterdump():
        tempfile.write('%s
' % line)
    con.close()
    tempfile.seek(0)

    # Create a database in memory and import from tempfile
    app.sqlite = sqlite3.connect(":memory:")
    app.sqlite.cursor().executescript(tempfile.read())
    app.sqlite.commit()
    app.sqlite.row_factory = sqlite3.Row

相关文章