Skip to content
Advertisement

How to get names of columns from table (Microsoft SQL Server)?

I need to get names of columns and save them in strings. SQL statement works correctly (I checked it in SQL manager).

  //program in QT

   QSqlQuery queryTem("tem");
   QSqlRecord rec1= queryTem.record();
   QString qs={"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME'"+tableName+"'"};
   queryTem.exec(qs);
       QString name1= ???

Advertisement

Answer

After exec() use queryTem.next() which retrieves next record in the result until theres any record. And use QVector to store column name. Read more here about QVector.

QVector<QString> columnNames;

while(queryTem.next())
{
    columnNames.push_back(queryTem.value(0).toString());
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement