How to get numbers of rows and columns using QtSql?

Mas Bagol picture Mas Bagol · Apr 16, 2015 · Viewed 7k times · Source

I have read the Qt documentation and can't find a direct way from the language itself to get numbers of rows and columns from the query result. The only way I can think of is using SELECT COUNT(*) FROM table_name in mysql query itself.

Like another connector (PHP, python, C++, etc.), as far as I know there's no direct way to get those values. Maybe I miss something. So, is there any possible way to do this? Maybe something like this:

int rows = db_connection->get_row_counts ();
int columns = db_connection->get_column_counts ();

Answer

Nejat picture Nejat · Apr 16, 2015

You can use QSqlQuery::size() to get the number of rows and QSqlRecord::count() to get the number of columns :

QSqlQuery qry;
qry.prepare("SELECT * FROM someTable");
qry.exec();

qDebug() << "Number of Rows: " << qry.size();
qDebug() << "Number of columns: " << qry.record().count();