0

I can't add a database to resources:

#include "QtSql/QSqlDatabase"
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(":/new/prefix1/database.db");
    if (!db.open())
           qDebug()<<"ERRR"; // this line is printed at startup

    return a.exec();
}

the database is present in resources:

enter image description here

aezk
  • 1
  • Since the resources are read only you may have to open the database in a read only mode. Is the data in the database never going to be modified? – drescherjm Jun 22 '20 at 13:34
  • @drescherjm _to open the database in a read only mode_ how to do this? – aezk Jun 22 '20 at 13:41
  • Related: [https://stackoverflow.com/questions/40127409/is-there-any-qt-sqlite-plugin-for-storing-database-in-ram-by-vfs-for-loading-da](https://stackoverflow.com/questions/40127409/is-there-any-qt-sqlite-plugin-for-storing-database-in-ram-by-vfs-for-loading-da) – drescherjm Jun 22 '20 at 13:46
  • And: [https://stackoverflow.com/questions/4254250/embedded-database-in-qt/4276580#4276580](https://stackoverflow.com/questions/4254250/embedded-database-in-qt/4276580#4276580) – drescherjm Jun 22 '20 at 13:47
  • @drescherjm it looks too complicated. Is there a way to just run open the database from the directory where the source code is located (main.cpp)?? After all, if you write `db. setDatabaseName ("database.db");` Qt will search for "database.db " in the build directory, not in the source code directory – aezk Jun 22 '20 at 14:05
  • There is no searching in the setDatabaseName() part. Although your program could instead use QDir to find the location of the file and use that as the argument to setDatabaseName(). – drescherjm Jun 22 '20 at 14:08
  • This answer does not look complicated at all to me: [https://stackoverflow.com/a/40284020/487892](https://stackoverflow.com/a/40284020/487892) – drescherjm Jun 22 '20 at 14:09

0 Answers0