Search code examples
c++sqliteqtqt5

How can I add an SQLite database permanently to the application


Currently, I am using an SQLite database this way:

QSqlDatabase Database; Database.addDatabase("QSQLITE");
Database.setDatabaseName("C:/Users/ARASH/Desktop/arash.db");

But, this isn't permanent. Because the application will not work for a different desktop folder under a different user.

I want my application to run on other machines as well. So, I need a permanent solution for generalizing database path for every computer. Like: we can add pictures to resource.qrc. I want to use database in that way.


Solution

  • You can use a DB path relative to your executable.

    For example, you have a directory structure like this:

    app folder
    - executable
    - data.db
    

    So, it would be:

    Database.setDatabaseName("data.db");
    

    Alternatively, you can use QStandardPaths class to get the DesktopLocation or other locations as per your requirements.