1

I want to package my Qt application with an existing Sqlite db. I have the standard Qt database code:

m_db = QSqlDatabase::addDatabase("QSQLITE"); 

m_db.setDatabaseName("database.sqlite");
bool connected = m_db.open();

if (!connected) {
    qDebug() << "Error: connection with database failed";
} else {
    qDebug() << "Database: connection success";
    QSqlQuery q(m_db);
    q.prepare("SELECT * FROM people");
    if (q.exec()) {
        qDebug() << "Yay!";
    } else {
        qWarning() << "Bad exec: " << q.lastError();
        qWarning() << q.executedQuery();
    }
}

However, the result is:

Error: connection with database failed

I know this is because it's not finding the correct database, and is instead creating a new one. If I provide the absolute path on my development machine to m_db.setDatabaseName(), it works. But my database file is in my .qrc and will not be available in that location when I deploy... so how can I get a reliable path for this database?

Rama
  • 3,222
  • 2
  • 11
  • 26
  • Is this what are you looking for?: https://stackoverflow.com/questions/4254250/embedded-database-in-qt – Nazar554 Feb 06 '17 at 08:50

1 Answers1

0

in the setDatabaseName-call use the right syntax for resource files:

m_db.setDatabaseName(":database.sqlite"); // <-- note the : before the name

... presuming the database file is located in the root of your resource file system.

Greetings, Thomas