Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 309
Carl Seech Avatar asked Nov 23 '10 08:11

Carl Seech


People also ask

Is SQLite an embedded database?

SQLite is an embedded, open-source, lightweight SQL database engine. The C based library is transactional, self-contained, and highly compact. It's also fairly easy to implement. It doesn't require any sort of installation or configuration, and all data is stored locally.

What is SQLite in Qt?

SQLite is a small C library that implements a self-contained, embeddable, zero-configuration SQL database engine. Used in Qt SQL Lite plugin.


3 Answers

Even if you solve you compilation problems, embedding a sqlite database in the qrc file will not work. See e.g. the discussion in the Qt Centre Forum or on the Qt-interest mailing list. 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.

like image 113
hmuelner Avatar answered Sep 24 '22 11:09

hmuelner


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

  1. Create a directory called "data" in your project directory.
  2. Put your database file in there.
  3. In your .pro file, add this section:

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

  4. 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"));
    
like image 25
Volomike Avatar answered Sep 25 '22 11:09

Volomike


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

like image 42
Sergio Avatar answered Sep 23 '22 11:09

Sergio