"Pickling" an in-memory SQLite database.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

"Pickling" an in-memory SQLite database.

Randall Smith
Hi, guys.

I have an application that allows me to embed a (potentially large) block of data into its application file at close, and read the block back on open.  It would be convenient and attractive for me, for a plugin I am writing for this application, to be able to use an in-memory SQLite database, and then squirrel the DB away with everything else in the application file.  That way, everything would be stored under one roof, so to speak.

My question:  Is there a good way to "pickle" a SQLite database into a block of bytes for storage, and then "unpickle" it later for use?  My fantasy is that this could all be done in memory so the file system does not have to become involved.

I feel like I am missing something obvious here, but can't quite come up with it.  Thanks for any ideas or suggestions.

Randall.

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: "Pickling" an in-memory SQLite database.

Shawn Wagner
sqlite3_serialize() and deserialize would work:
https://www.sqlite.org/c3ref/serialize.html

On Thu, Jan 31, 2019, 4:47 PM Randall Smith <[hidden email] wrote:

> Hi, guys.
>
> I have an application that allows me to embed a (potentially large) block
> of data into its application file at close, and read the block back on
> open.  It would be convenient and attractive for me, for a plugin I am
> writing for this application, to be able to use an in-memory SQLite
> database, and then squirrel the DB away with everything else in the
> application file.  That way, everything would be stored under one roof, so
> to speak.
>
> My question:  Is there a good way to "pickle" a SQLite database into a
> block of bytes for storage, and then "unpickle" it later for use?  My
> fantasy is that this could all be done in memory so the file system does
> not have to become involved.
>
> I feel like I am missing something obvious here, but can't quite come up
> with it.  Thanks for any ideas or suggestions.
>
> Randall.
>
> _______________________________________________
> sqlite-users mailing list
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: "Pickling" an in-memory SQLite database.

Thomas Levine
If sqlite3_serialize and sqlite3_deserialize are not exposed in your bindings, I suppose you can do something like

with NamedTemporaryFile(mode='rb', prefix=prefix) as tmp:
    c = sqlite3.connect(tmp.name)
    # Do stuff.
    c.close()
    serialized = tmp.file.read()

where "prefix" is a RAM filesystem.

sqlite3_serialize and sqlite3_deserialize are better though.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users