在python中,如何在连接到数据库之前将sqlite db完全加载到内存中? [英] In python, how can I load a sqlite db completely to memory before connecting to it?

查看:375
本文介绍了在python中,如何在连接到数据库之前将sqlite db完全加载到内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个100兆字节的sqlite db文件,我希望在执行sql查询之前将其加载到内存中.可以在python中做到吗?

I have a 100 mega bytes sqlite db file that I would like to load to memory before performing sql queries. Is it possible to do that in python?

谢谢

推荐答案

apsw 是用于sqlite,可让您在执行操作之前将磁盘上的数据库备份到内存中.

apsw is an alternate wrapper for sqlite, which enables you to backup an on-disk database to memory before doing operations.

文档:

###
### Backup to memory
###

# We will copy the disk database into a memory database

memcon=apsw.Connection(":memory:")

# Copy into memory
with memcon.backup("main", connection, "main") as backup:
    backup.step() # copy whole database in one go

# There will be no disk accesses for this query
for row in memcon.cursor().execute("select * from s"):
    pass

上面的

connection是您的磁盘数据库.

connection above is your on-disk db.

这篇关于在python中,如何在连接到数据库之前将sqlite db完全加载到内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆