[Solved]How to check if column exists in sqlite in Qt
-
wrote on 11 May 2015, 07:20 last edited by Abin
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.
-
wrote on 11 May 2015, 10:20 last edited by
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(); }
-
wrote on 17 May 2015, 15:08 last edited by
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/3