Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

c++ - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...