Python中的内存数据库 [英] in-memory database in Python

查看:175
本文介绍了Python中的内存数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中对大型数据库进行一些查询,以便从数据库中获取一些统计信息。我想要这些统计信息在内存中,以便其他程序可以使用它们,而无需去数据库。

I'm doing some queries in Python on a large database to get some stats out of the database. I want these stats to be in-memory so other programs can use them without going to a database.

我在想如何构建它们,并且在尝试设置一些复杂的嵌套字典之后,我意识到一个好的表示将是一个SQL表。我不想把数据存回到持久性数据库,虽然。是否有任何内存实现的SQL数据库支持使用SQL语法查询数据?

I was thinking of how to structure them, and after trying to set up some complicated nested dictionaries, I realized that a good representation would be an SQL table. I don't want to store the data back into the persistent database, though. Are there any in-memory implementations of an SQL database that supports querying the data with SQL syntax?

推荐答案

SQLite3可能工作。 Python界面支持 SQLite3 C API提供的内存实现。

SQLite3 might work. The Python interface does support the in-memory implementation that the SQLite3 C API offers.

根据规格:


您也可以提供特殊名称
:memory:在RAM中创建一个数据库。

You can also supply the special name :memory: to create a database in RAM.

这取决于你在做什么。要开始,只需:

It's also relatively cheap with transactions, depending on what you are doing. To get going, just:

import sqlite3
conn = sqlite3.connect(':memory:')

您可以像使用常规数据库一样继续操作。

You can then proceed like you were using a regular database.

根据您的数据 - 如果可以使用键/值(字符串,哈希,列表,集合,排序集等) - Redis 可能是另一个选择来探索(如你所说,你想与其他程序共享)。

Depending on your data - if you can get by with key/value (strings, hashes, lists, sets, sorted sets, etc) - Redis might be another option to explore (as you mentioned that you wanted to share with other programs).

这篇关于Python中的内存数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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