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

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

  • 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();

  • 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));

Log in to reply