简体   繁体   中英

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= ???

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM