Search code examples
c++qtqt5qsqlquery

What is the difference between void QSqlQuery::clear() and void QSqlQuery::finish() in Qt5?


The documentation is kind of vague on the subject:

From void QSqlQuery::clear():

Clears the result set and releases any resources held by the query. Sets the query state to inactive. You should rarely if ever need to call this function.

From void QSqlQuery::finish():

Instruct the database driver that no more data will be fetched from this query until it is re-executed. There is normally no need to call this function, but it may be helpful in order to free resources such as locks or cursors if you intend to re-use the query at a later time.

Sets the query to inactive. Bound values retain their values.

Does one imply the other? Which resources will be freed in each case? Why would I use one instead of the other?


Solution

  • I think the idea is rather clear and functions does not imply each other. First one does clear everything:

    Clears the result set and releases any resources held by the query

    After that you won't be able to get any information on this query, while the second one just marks a query as inactive (well, it just "says" that it's empty) and prepares the query to be ready for being

    1. re-executed
    2. cleared

    Bound values retain their values

    It means that at least some data is still accessible after the execution of the finish().

    If you don't want to create another QSqlQuery object and want to execute a completely different query, then you may use clear() and not be afraid of any memory leaks or something else. But if you are planning to execute the same query later, for example with another set of bound values, you may use finish(). It will also release some driver's internal resources (as it's said in the documentation).