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. How to check if Sqlite Database is valid?
QtWS25 Last Chance

How to check if Sqlite Database is valid?

Scheduled Pinned Locked Moved Solved General and Desktop
sqlitesqlite3sqlite database
5 Posts 3 Posters 4.0k 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.
  • O Offline
    O Offline
    Opa114
    wrote on 25 Dec 2018, 17:58 last edited by Opa114
    #1

    How can i check if a loaded (via file dialog) sqlite database is

    • a valid sqlite Database (e.g. version 3)
    • if the sqlite database is valid, did the content (columns in the table) did match to the ones in my create statement

    For exampe my table inside my sqlite databse is created like this:

    QSqlQuery query;
    query.prepare("CREATE TABLE IF NOT EXISTS myTable"
                      "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
                      "data BLOB NOT NULL UNIQUE, "
                      "moreData BLOB, "
                      "moreData2 BLOB, "
                      "date DATETIME DEFAULT CURRENT_TIMESTAMP)");
    query.exec();
    

    There are any built in tools or commands for this? Or mst implement it by myself. If yes, how can i do this?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrdebug
      wrote on 25 Dec 2018, 18:56 last edited by mrdebug
      #2

      Please have a look
      http://doc.qt.io/qt-5/qsqlquery.html#exec
      exec() returns true or false.
      If false lastError() contains the error as QSqlError.

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 25 Dec 2018, 20:36 last edited by
        #3

        Hi,

        To add to @mrdebug, the QSQLITE driver is for SQLite 3, you have to explicitly use a different driver for SQLite 2.
        If a database file cannot be found, it will be created. That's nothing Qt specific, it's how SQLite works. If you want to know whether you start from a new empty db or from an existing one, the you have to first check if the file exists.

        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
        2
        • O Offline
          O Offline
          Opa114
          wrote on 25 Dec 2018, 22:49 last edited by
          #4

          Thanks for reply.
          @mrdebug so you mean i could make a query a do a select on all my columns i want to check if them available? and if i got an error there i know the loaded database is not correct?

          I found the tables() function, which i can run on my QSqlDatabase-Object andthen with exists i can check if a specific table exists - this check for the columns inside my table would be great.

          @SGaist ah thanks for the hint with the driver, that is good to know, so tehre i'm fine to load the correct version of an sqlite database.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrdebug
            wrote on 25 Dec 2018, 23:04 last edited by
            #5

            Hi, I suggest you to create a class to manage exceptions, so if you have a situation like this

            if (!MyQuery.Exec("select ..")) GeneralException.raise();
            if (!MyQuery.Exec("select ..")) GeneralException.raise();
            if (!MyQuery.Exec("select ..")) GeneralException.raise();

            if a query fails you will jump in the exception function manager.

            If you want to have the list of the columns present in a sqlite database you should use the sqlite features, so
            "pragma table_info(Table01)"
            To have the list of the tables I think there is a similar way.

            Regards.

            Need programmers to hire?
            www.labcsp.com
            www.denisgottardello.it
            GMT+1
            Skype: mrdebug

            1 Reply Last reply
            1
            • S sierdzio referenced this topic on 19 Jun 2023, 05:51

            3/5

            25 Dec 2018, 20:36

            • Login

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