QT MAC app cannot read SQLite database.



  • Database settings:

    In. h file.
    QSqlDatabase db;

    In the CPP file.
    This - > DB = QSqlDatabase:: addDatabase ("QSQLITE");
    Db. setDatabaseName (". / realtest. db");

    Under win, debug, release. it's works well

    However, after migrating to Mac, it was normal at debug. However, after being published as app, the data could not be read.
    If absolute paths are adopted, such as:
    DB = QSqlDatabase:: addDatabase (ReadingSqlConst:: SQLITE);
    Db. setDatabaseName (QString ("/Users/apple/Desktop/newztdb/Resources/realtest.db");
    it's works well, but it's notwork if you take the following path

    Method 1:
    DB = QSqlDatabase:: addDatabase (ReadingSqlConst:: SQLITE);
    Db. setDatabaseName (QString (QApplication:: Application DirPath ()+"/ Resources / realtest. db");

    Method 2:
    QDir tempDir;
    QString current Dir = tempDir. current Path ();
    DB = QSqlDatabase:: addDatabase (ReadingSqlConst:: SQLITE);
    Db. setDatabaseName (currentDir +"/ Resources / realtest. db");
    Mode 1 and mode 2 are also abnormal.

    I just want to put sqlite's DB file in app. How can I solve it?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You forgot to go up one folder from the application folder.

    However, that said, you are taking a wrong approach. Forcing the file to be at the same level as your executable is bound to fail in the future. Usually applications are installed in read-only directories be it Linux, macOS or Windows. You should rather use QStandardPaths::writableLocation to get a folder that is specific for the user of the application.



  • @sgaist said in QT MAC app cannot read SQLite database.:

    Hi and welcome to devnet,

    You forgot to go up one folder from the application folder.

    However, that said, you are taking a wrong approach. Forcing the file to be at the same level as your executable is bound to fail in the future. Usually applications are installed in read-only directories be it Linux, macOS or Windows. You should rather use QStandardPaths::writableLocation to get a folder that is specific for the user of the application.

    Does that mean that I can't put sqlite db in the applications, but only in other directories, such as user documents? Is there any demo, I'll refer to it.


  • Lifetime Qt Champion

    That's not what I wrote. I wrote that applications are usually stored in read-only folders so writing a database there will fail. Furthermore, these folders are usually machine centric which mean that depending on what you implement several users will read/write/overwrite data of other users.

    As for macOS see Apple's Filesystem Overview.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.