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. [Solved]How to check if column exists in sqlite in Qt
Forum Update on Monday, May 27th 2025

[Solved]How to check if column exists in sqlite in Qt

Scheduled Pinned Locked Moved General and Desktop
sqlite
3 Posts 2 Posters 7.1k 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.
  • A Offline
    A Offline
    Abin
    wrote on 11 May 2015, 07:20 last edited by Abin
    #1

    I have an application which uses sqlite database. I updated the application and needed to update my database on application launch.

    For this I need to check if some column exists in a table. I am not sure how to do it...

    I saw PRAGMA table_info(table-name); will return column names but that result is in a table and I am not sure how to read it in Qt.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 11 May 2015, 10:20 last edited by
      #2

      HI,

      you can use the QSqlRecord and `QSqlField' classes for that.

      QSqlQuery q("SELECT * FROM table_name");
      QSqlRecord r = r.record();
      
      for (int i = r.count() ++i) {
        qDebug() << r.field(i).name();
      }
      

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Abin
        wrote on 17 May 2015, 15:08 last edited by
        #3

        Thanks mcosta.
        I got this info and followed it.
        PRAGMA table_info returns its data like a normal query, i.e., as if there were a query SELECT cid, name, type, notnull, dflt_value, pk FROM ...:

        query.exec("PRAGMA table_info(MyLittleTable)");
        while (query.next()) {
        print("column name: ", query.value(1));
        }

        1 Reply Last reply
        0

        1/3

        11 May 2015, 07:20

        • Login

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