Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt clearing an SQL query

Tags:

qt

What is the difference between

void QSqlQuery::clear ()

and

void QSqlQuery::finish ()

Based on the documentation, I don't see what the diff is. What is the difference? I'd like to know specifically when to use one over the other.

EDIT - Some more elaboration and info from documentation.
clear()
-Clears the result set and releases any resources held by the query.
Sounds like finish() does the same...

-Sets the query state to inactive.
Finish does the same.

finish()
-Instruct the database driver that no more data will be fetched from this query until it is re-executed.
What does this mean specifically? What is the consequence of this?

-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.
Doesn't clear do the same? Doesn't clear release locks, cursors, etc?

-Sets the query to inactive.
clear does the same I believe.

-Bound values retain their values.
What is the point of this?

like image 550
user440297 Avatar asked Aug 25 '26 01:08

user440297


2 Answers

Qt comes with source code, you can see what's the difference by simply looking into the qsqlquery.cpp file

So according to the source code:

  • clear - clears and resets the QSqlQuery object;
  • finish - resets the result member of the current query into inactive state;

hope this helps, regards

like image 138
serge_gubenko Avatar answered Aug 28 '26 05:08

serge_gubenko


The language used to describe these functions is similar so it can definitely be a little confusing and I hope this explanation helps. Here's how I interpret and use these methods.

void QSqlQuery::finish ()

I think of this as a way of saying I'm done with the query I just requested (eg no more reading/iterating) but I still plan on using that QSqlQuery object to do more work. You're just releasing any memory/resources used to get the values from the previous query. This really only makes a big, noticeable difference when you're dealing with large datasets over and over again, but I view it as good practice to use none the less.

void QSqlQuery::clear ()

This is my way of saying that I'm done with the QSqlQuery object and want to guarantee that none of the resources/memory I was using gets left around while I'm disposing of the object. I rarely, if ever, use this as I've found that it's effectiveness can vary widely depending on the database you use and if you're using modern C++ features, it doesn't do a lot for you.

It's easier to understand the difference if you look at them as being written to solve a similar problem for two different time periods (eg old C code as opposed to modern C++).

They do very similar things but I'd recommend you just use finish().

like image 45
mabeechen Avatar answered Aug 28 '26 05:08

mabeechen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!