Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT MAC app cannot read SQLite database.
Forum Update on Monday, May 27th 2025

QT MAC app cannot read SQLite database.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 533 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    m2009
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        M Offline
        M Offline
        m2009
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved