How to load existing db file to memory in Python sqlite3?(如何在 Python sqlite3 中将现有的 db 文件加载到内存中?)
问题描述
我有一个现有的 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
这篇关于如何在 Python sqlite3 中将现有的 db 文件加载到内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Python sqlite3 中将现有的 db 文件加载到内存中?


基础教程推荐
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01