#1 2011-09-27 22:27:52

oruchreis
Member
Registered: 2011-09-27
Posts: 1

SQLite database in TMemoryStream

Hi,
Can I load an sqlite database from a memory stream, or can I use a memory pointer as a database file? I need to reach an sqlite database file in the memory without extracting it into a file. It must remain in the memory when I load the database.
Regards.

Last edited by oruchreis (2011-09-27 22:28:30)

Offline

#2 2011-09-28 05:10:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: SQLite database in TMemoryStream

You can have in-memory SQLite3 database by using ':memory:' as file name.

So you can open an existing database, then copy into this memory database, and work in it.
But AFAIK there is no direct 'in-memory opening' of a database file.

This is nothing special to our libraries, just a standard SQLite3 feature.
To make the copy, you can use the backup API - see http://www.sqlite.org/backup.html
or use the techniques described in http://www.sqlite.org/cvstrac/wiki?p=InMemoryDatabase

Offline

#3 2011-09-30 02:49:23

steadfast
Member
Registered: 2011-09-30
Posts: 2

Re: SQLite database in TMemoryStream

I'm using mORMot as an application file (like a document in a word-processor) and would also like to have the functionallity of copying a database between disk and memory.  Could you please let us know if you get this working.

Offline

#4 2011-09-30 05:21:16

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: SQLite database in TMemoryStream

It depends on the database backend you are using.

If you use our internal TSQLRestServerStaticInMemory class, it would fit exactly your need: can be stored as JSON or binary files on disk, and will work in memory. You only update the file if you want to.
You can even use the SQLite3 engine over it. That is, if you use TSQLVirtualTableBinary or TSQLVirtualTableJSON, your TSQLRestServerStaticInMemory in-memory instances can be accessed using the SQLite3 SQL engine, not its database B-Tree engine.

 {{ A TSQLRestServerStaticInMemory-based virtual table using JSON storage
   - for ORM access, you should use TSQLModel.VirtualTableRegister method to
     associated this virtual table module to a TSQLRecordVirtualTableAutoID class
   - transactions are not handled by this module
   - by default, no data is written on disk: you will need to call explicitly
     aServer.StaticVirtualTable[aClass].UpdateToFile for file creation or refresh
   - file extension is set to '.json' }
  TSQLVirtualTableJSON = class(TSQLVirtualTable)

There is dedicated documentation about those classes and the Virtual Table mechanism it relies on.
See http://synopse.info/fossil/wiki?name=Downloads

The only caveat of this TSQLRestServerStaticInMemory implementation is that it will generate one file per table.
We may create some TSQLVirtualTableBinary child which will store the binary content into a .zip container. It will add compression (optionally disabled if speed if a must), and only one file per database.
Sounds a bit like reinventing the wheel, but it has been done step by step, due to the power of SQLite3 virtual tables, not from evil intention... wink

Offline

Board footer

Powered by FluxBB