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. SQLite3 and "pragma application_id"
Qt 6.11 is out! See what's new in the release blog

SQLite3 and "pragma application_id"

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.6k Views 2 Watching
  • 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.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by
    #1

    I am trying to programmatically determine whether a SQLite database was created by my application or not. There is a feature in SQLite: "PRAGMA application_id". When I create the database file using QSqlDatabase etc., I set this value to a magic number, and if I open the database in the sqlite3 console, I get the correct value returned when I use this command.

    However, when I issue "PRAGMA application_id" as a query in QSqlQuery, there is a different value returned entirely. It is documented that the value is stored in a four-byte range starting at offset 68, so I can easily use the standard file I/O operations to check it. However, I am wondering why the query is not returning the value correctly?

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

      Hi,

      Can you show the code you are using to handle your database and application id ?

      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
      • R Offline
        R Offline
        Robert Hairgrove
        wrote on last edited by
        #3

        Hello,
        Here is the code I use to set the application_id:

            // assume that db is a valid and open QSqlDatabase object:
            sql = "PRAGMA application_id=%1";
            // this can be any 32-bit signed integer value,
            // according to the SQLite documentation.
            // In my code it is defined as 1,396,851,276 which is within the range:
            sql = sql.arg(SBBL_MAGIC_NUMBER);
            db.exec(sql);
        

        When I open the file in the sqlite3 terminal app, I see this same number. But when I run the query like this:

            bool own_app_db = false;
            sql = "PRAGMA application_id";
            if ((retval = Q.exec())) {
              while (Q.next()) {
                bool ok = false;
                int magic_number = Q.value(0).toInt(&ok);
                own_app_db = (magic_number == SBBL_MAGIC_NUMBER);
                retval = own_app_db;
                if (!own_app_db) {
                  erm = tr("The SQLite database was not an SBBL database.");
                }
              }
            }
        

        In the debugger, I get the value 153 and see that "ok" is set to "true". Perhaps "PRAGMA" commands return values differently than regular queries?

        Thanks for looking into this!

        R 1 Reply Last reply
        0
        • R Robert Hairgrove

          Hello,
          Here is the code I use to set the application_id:

              // assume that db is a valid and open QSqlDatabase object:
              sql = "PRAGMA application_id=%1";
              // this can be any 32-bit signed integer value,
              // according to the SQLite documentation.
              // In my code it is defined as 1,396,851,276 which is within the range:
              sql = sql.arg(SBBL_MAGIC_NUMBER);
              db.exec(sql);
          

          When I open the file in the sqlite3 terminal app, I see this same number. But when I run the query like this:

              bool own_app_db = false;
              sql = "PRAGMA application_id";
              if ((retval = Q.exec())) {
                while (Q.next()) {
                  bool ok = false;
                  int magic_number = Q.value(0).toInt(&ok);
                  own_app_db = (magic_number == SBBL_MAGIC_NUMBER);
                  retval = own_app_db;
                  if (!own_app_db) {
                    erm = tr("The SQLite database was not an SBBL database.");
                  }
                }
              }
          

          In the debugger, I get the value 153 and see that "ok" is set to "true". Perhaps "PRAGMA" commands return values differently than regular queries?

          Thanks for looking into this!

          R Offline
          R Offline
          Robert Hairgrove
          wrote on last edited by
          #4

          Oops, now I see that I forgot to give the query the SQL statement entirely! Still it is strange that there was no error...?

          Anyhow, it is now working!

          (Grüezi nach Sion ... ich bin in Zürich, und bald in Winterthur)

          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