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.
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:".
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).
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.
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.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With