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

I have a database :/test.sqlite3 inside .qrc . And the goal is to directly use this database in the program. The database is read-only.

QSqlDatabase::setDatabase(":/test.sqlite3") does not work because Qt SQLite is not designed to work with the Qt file system.

One solution is to copy the database from .qrc to D:\temdb.sqlite3 and use it with QSqlDatabase::setDatabase("D:\\temdb.sqlite3") . But the program should not work with the OS file system.

The second solution stores :/dump.sql in resources, and then creates the database in memory using QSqlDatabase::setDatabase(":memory:") and imports a dump into it, reading and executing the lines from :/dump.sql . But this method is slow.

And finally, a tough but true way is to create your own Qt plugin for SQLite with a VFS implementation for reading the database from RAM, where we have bytes ":/test.sqlite3" .

Is there another easy way?

PS I already read all the questions, such as Converting a sqlite database into memory into a blob / char array and others, so do not mark it as a duplicate. My question is about any other methods.

+7
sqlite qt qt5 vfs
source share
1 answer

You can use Qt QTemporaryFile to copy your data and get started. QTemporaryfile works on all os.

Here is an example (this temporary file is associated with all qApp so that it is deleted after exiting 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(); } 

You can open the tmpFile.fileName() file as QSqlDatabase :

 QSqlDatabase::setDatabase(tmpFile.fileName()); 
+1
source share

All Articles