0

I'm using Qt Creator, and I've made a "Qt Widgets Application" subproject.

I'm trying to open an SQLite database that would ideally be read-only. The file is currently located in the same directory as the source code:

.
├── myApp
│   ├── main.cpp
│   ├── mydatabase.db
│   ├── myApp.pro
│   ├── resources.qrc
│   ├── widget.cpp
│   ├── widget.h
│   └── widget.ui
├── myApp_sdp.pro
├── myApp_sdp.pro.user

The file is included in resources.qrc:

<RCC>
    <qresource prefix="/db">
        <file>mydatabase.db</file>
    </qresource>
</RCC>

Here's the code I've tried running both in the main function and in the Widget constructor, if that makes any difference:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":/db/mydatabase.db");
if (!db.open())
    cout << "Couldn't open database" << endl;
else
    cout << "Database open" << endl;
db.close();

The database doesn't open.

From what I understand, not all functions understand the ":" prefix. I've tried asking chatGPT for help but its solution did not work:

QFileInfo fileInfo(resourcePath);
QString filePath = fileInfo.absoluteFilePath();
db.setDatabaseName(filePath);

Is there a way to make this work other than shipping the executable with a separate .db file? The program only needs to read the file.

Rijaja
  • 11
  • 1
  • 3

0 Answers0