Warm tip: This article is reproduced from serverfault.com, please click

How to get numbers of rows and columns using QtSql?

发布于 2015-04-16 04:25:09

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 ();
Questioner
Mas Bagol
Viewed
0
Nejat 2015-04-16 12:44:29

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