Embedded Database in Qt

I have a SQLite database for my Qt application. I suppose it would be logical to add the database as a resource.

I cannot get my application to compile with an embedded resource.

connection.h

#ifndef CONNECTION_H #define CONNECTION_H #include <QMessageBox> #include <QSqlDatabase> #include <QSqlError> #include <QSqlQuery> static bool createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(":/data/ShippingData.db3"); if (!db.open()) { QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text()); return false; } return true; } #endif // CONNECTION_H 

assets.qrc

 <RCC> <qresource prefix="/data"> <file>ShippingData.db3</file> </qresource> </RCC> 

My sqlite database right now looks like

  app.pro file.h file.cpp data/ShippingData.db3 

Build Error (from Qt Creator)

 No rule to make target `../TimePlotter/Shipping.db3', needed by `debug/qrc_assets.cpp'. Stop. 

I tried to change the layout of the resource, because the compiler from the message does not enter the data / folder folder where the database is located. I get exactly the same build problem with this resource file

 <RCC> <qresource> <file>data/ShippingData.db3</file> </qresource> </RCC> 

TimePlotter.pro

 #------------------------------------------------- # # Project created by QtCreator 2010-11-21T03:18:17 # #------------------------------------------------- QT += core gui TARGET = TimePlotter TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp \ time.cpp \ clients.cpp \ printTime.cpp HEADERS += mainwindow.h \ time.h \ clients.h \ printTime.h \ connection.h FORMS += mainwindow.ui \ time.ui \ clients.ui \ printTime.ui RESOURCES += \ assets.qrc 
+5
c ++ sqlite qt embedded-resource
source share
3 answers

Even if you are solving compilation problems, embedding the sqlite database in the qrc file will not work. See the discussion in the Qt Center Forum or on the Qt-interest mailing list . The best solution would be IMHO to include the database dump in the qrc file, create the sqlite db database and rebuild the database from the SQL statements in the resource.

+8
source share

It seems that you deleted or renamed your db file Shipping.db3 and added ShippingData.db3. To fix this build problem, you must delete the build folder and rebuild the project. This should solve your build problem.

You can read the instructions for deploying the database here: http://discussion.forum.nokia.com/forum/showthread.php?202894-Add-existing-Sqlite-database-to-Qt-project

+1
source share

At least I know how to do this on Mac OSX, where the QMAKE_BUNDLE_DATA parameter works. For Windows, open this answer .

  • Create a directory named "data" in the project directory.
  • Put your database file there.
  • In your .pro file, add this section:

    mac { Resources.files = data Resources.path = Contents/MacOS QMAKE_BUNDLE_DATA += Resources }

  • Now, when you rebuild the application, it will be located in the Contents / MacOS / data folder. This way you can do something similar if your database was named custom.db:

     db.setDatabaseName(QCoreApplication::applicationDirPath().append("/data/custom.db")); 
+1
source share

All Articles