Dealing with queries returning multiple result sets

· by alarsen · 1 min read · original post ↗

·

Some databases allow stored procedures and SQL batches to return multiple result sets. As some of you may have noticed Qt only allows you to access a single result set even when your query returns multiple, but as of Qt 4.4 this limitation will be history.

The cause of this joy is the newborn QSqlQuery::nextResult() which behaves very similar to QSqlQuery::next() with the exception that it navigates to the next result set instead of the next row. When a new result set is available it returns true and you can use next() as usual to iterate over its rows. Note that a result set can be empty and that a count of affected rows instead of a result set can be returned for non-SELECT statements.

A note of caution: not all databases are equal. For example some databases may execute all the statements in a stored procedure or SQL batch at once while others may delay the execution of each individual statement until the result set of the statement is actually accessed. Also, some databases may restrict which statements are allowed to be used in a stored procedure or SQL batch. If you run into problems you should first check what QSqlQuery::lastError() returns and then consult the documentation of your database.

As usual you should use QSqlDriver::hasFeature() to check whether the driver you’re using supports this feature or not. Currently nextResult() is implemented in the MySQL, ODBC and DB2 drivers.

If you’re too excited to wait for Qt 4.4 to play with this feature you can check out the snapshot for some adventurous fun.

Qt

More on Qt

More from alarsen