Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any Qt SQLite plugin for storing database in RAM by VFS (for loading database from Qt resource file)?

Tags:

sqlite

qt

qt5

vfs

I have some :/test.sqlite3 database inside .qrc. And the goal is to directly use this database in program. Database used only for reading.

QSqlDatabase::setDatabase(":/test.sqlite3") not works, because Qt SQLite not designed for working with Qt's filesystem.

One of solutions is copying database from .qrc into D:\temdb.sqlite3 and using it by QSqlDatabase::setDatabase("D:\\temdb.sqlite3"). But program must not work with OS filesystem.

Second solution is storing :/dump.sql in resources, then creating in-memory database by QSqlDatabase::setDatabase(":memory:") and importing dump into it by reading and executing lines from :/dump.sql. But this method is slow.

And, finally, hard but true way is creating own Qt plugin for SQLite with VFS implementation for reading database from RAM, where we have bytes of ":/test.sqlite3".

Is there any another easy way?

P.S. I have already read all questions like Converting in-memory sqlite database to blob/char array and other, so don't mark it as duplicate. My question is about any other methods.

like image 756
Rinat Avatar asked Oct 19 '16 09:10

Rinat


People also ask

Does SQLite load database into memory?

An SQLite database is normally stored in a single ordinary disk file. However, in certain circumstances, the database might be stored in memory. The most common way to force an SQLite database to exist purely in memory is to open the database using the special filename ":memory:".

What is SQLite plugin?

SQLite Plugin is a plugin for the 4th Dimension programming language that gives you an embedded SQL database engine contained in a single plugin (no additional software needed).

How much data we can store in SQLite?

SQLite database files have a maximum size of about 140 TB. On a phone, the size of the storage (a few GB) will limit your database file size, while the memory size will limit how much data you can retrieve from a query. Furthermore, Android cursors have a limit of 1 MB for the results.

Does SQLite store data?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases. However, there are no restrictions on creating databases elsewhere.


1 Answers

You could take advantage of Qt QTemporaryFile to copy your data and start. QTemporaryfile works on every os.

Here is an example (this temporary file is associated to the entire qApp so that it will be removed once you quit the application):

QTemporaryFile tmpFile(qApp);
tmpFile.setFileTemplate("XXXXXX.sqlite3");
if (tmpFile.open()) {
    QString tmp_filename=tmpFile.fileName();
    qDebug() << "temporary" << tmp_filename;

    QFile file(":/test.sqlite3");
    if (file.open(QIODevice::ReadOnly)) {
        tmpFile.write(file.readAll());
    }

    tmpFile.close();
}

The you can reopen the file tmpFile.fileName() as QSqlDatabase:

QSqlDatabase::setDatabase(tmpFile.fileName());
like image 123
bibi Avatar answered Nov 15 '22 05:11

bibi