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.
我有一個現有的sqlite3 db文件,我需要進行一些廣泛的計算。從文件中進行計算是非常緩慢的,而且由於文件不是很大(大約10 MB),所以將它加載到內存中應該沒有問題。
Is there a Pythonic way to load the existing file into memory in order to speed up the calculations?
是否有一種python化的方式將現有文件加載到內存中以加速計算?
7 个解决方案
#1
90
Here is the snippet that I wrote for my flask application:
以下是我為flask應用程序編寫的代碼片段:
import sqlite3
from StringIO 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\n'