6

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.

Community
  • 1
  • 1
Rinat
  • 1,941
  • 2
  • 17
  • 26
  • "Easy" way, no. But Sqlite comes with its own VFS abstraction, so in theory it's possible to get it to look straight into Qt's resources. – peppe Oct 20 '16 at 17:37
  • @peppe are there any examples? Can't believe that nobody coded that yet :) – Rinat Oct 21 '16 at 06:22
  • If you plan on keeping the entire data set in memory, using SQL to access that is probably the least efficient course you can take. You can directly read in the data into regular C++ containers and enjoy much better access times. – dtech Oct 24 '16 at 19:07
  • Of course, I've already thought about that and this question wouldn't been asken if I decided that using C++ containers is suitable solution. Data in SQLite db is too complicated for manipulating using standard containers – Rinat Oct 25 '16 at 06:37
  • Actually relational databases are rather simple in their underlying data structure. Scenarios which merit db usage are when you need persistence and access from multiple users, a read only in memory data set makes db usage a waste. It is like paying a banker to manage the money you keep in your pocket. You end up wasting money and time on an unnecessary service. Do as you will, but know that you are focusing on the wrong optimization. – dtech Oct 25 '16 at 11:19
  • Anyway, I agree with you. Maybe in the future I'll remake project for using native C++ data structs. At this moment all logic done for sql data. By the way, data is tree-like. Maybe storing data in JSON is a good idea. So, I decided to modify existing solution (if it exists), because it is easier (hoping) than remaking data structure and its item model. – Rinat Oct 25 '16 at 12:18
  • I have researched this a while ago and came to this conclusion: http://stackoverflow.com/questions/4254250/embedded-database-in-qt/4276580#4276580 – hmuelner Oct 29 '16 at 10:24

1 Answers1

4

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());
bibi
  • 3,671
  • 5
  • 34
  • 50