7

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
Carl Seech
  • 95
  • 1
  • 5
  • Could you please post your .pro file also? It would help to understand. Thanks. – Symbiosoft Nov 23 '10 at 15:16
  • Also, where is located your assets.qrc file in your project tree? – Symbiosoft Nov 23 '10 at 15:20
  • Does posting the .pro contents answer your second question? In the actual directory assets.qrc is at the root of the project folder. (i.e. .cpp, .h, and .ui files in the same folder as the .qrc) – Carl Seech Nov 25 '10 at 03:01

3 Answers3

13

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.

hmuelner
  • 8,093
  • 1
  • 28
  • 39
2

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"));
    
Volomike
  • 23,743
  • 21
  • 113
  • 209
1

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

Sergio
  • 1,702
  • 1
  • 20
  • 24