简体   繁体   中英

How to deploy Qt application with an existing Sqlite db?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM