简体   繁体   中英

how to store a SQL-database in a python object, and perform queries in the object?

I have a big postgrSQL database. I would like to somehow store the full database in a python object, which form/structure would reflect the one of the database. Namely I imagine something like * An object database, with an attribute .tables which a kind of list of object "table", and a table object has an attribute "list_of_keys" (list of the column names) and an attribute "rows", which reflects all the rows of the corresponding table in the database.

Now, the main point i need is: i want to be able to perform a search in the database object, with exactely the same SQL synthax that i would use in the corresponding SQL database. Thus something like database.execute("SELECT * FROM .....") where, i repeat, "database" is a purely python object (which was filled with data coming from an SQL database, but which is now independent of it).

My aim is: i want to be able to apply the same algorithm either on a SQL-Database, or on a python-object, such as described above, without changing my code. So, i imagine, let "database" be either a usual database-connector/cursor (like with psycopg, for example), or a python object as i described, and the same piece of code database.execute("SELECT BLABLABLA") would work in both cases.

Is there any known module which allows that ?

thanks.

It might get a bit complicated, but take a look at SQLite's in-memory storage:

import sqlite3
cnx = sqlite3.connect(':memory:')
cnx.execute('CREATE TABLE ...')

There are some differences in the SQL syntax, but the basic stuff works fine. This might also take a good amount of RAM, depending on your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM