Carl Seech
Carl Seech

Reputation: 95

Embedded Database in Qt

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

I can't get my app to compile with the 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 is like this

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

Build Issue (From Qt Creator)

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

I tried changing my resource layout because it from the message the compiler isn't going into the data/ folder where the database is. I get the exact same build issue 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

Upvotes: 7

Views: 5458

Answers (3)

Volomike
Volomike

Reputation: 24886

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

  • Create a directory called "data" in your project directory.

  • Put your database file in there.

  • In your .pro file, add this section:

    mac {
    Resources.files = data
    Resources.path = Contents/MacOS
    QMAKE_BUNDLE_DATA += Resources
    }
    
  • Now when you rebuild your application, it will be located in the Contents/MacOS/data folder. Thus, you could do something like this if your database was named custom.db:

      db.setDatabaseName(QCoreApplication::applicationDirPath().append("/data/custom.db"));
    

Upvotes: 2

hmuelner
hmuelner

Reputation: 8221

Even if you solve you compilation problems, embedding a SQLite database in the qrc file will not work.

The best solution would be IMHO to include a dump of a database in the qrc file, create a memory SQLite db and rebuild the database from the SQL statements in the resource.

Upvotes: 13

Sergio
Sergio

Reputation: 1732

It seems that you removed or renamed your db file Shipping.db3 and added ShippingData.db3. To fix this build issue you should delete your build folder and rebuild project. This should solve your build issue.

Database deployment instructions you can read here: http://discussion.forum.nokia.com/forum/showthread.php?202894-Add-existing-Sqlite-database-to-Qt-project

Upvotes: 1

Related Questions