Search code examples
c++mysqlqtqtsqlqsqlquery

How to get numbers of rows and columns using QtSql?


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

Solution

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