0

Can I use the same QSQLQuery variable to execute multiple statements in Qt 5.3.2 using SQLite? Should I call the finish or the clear function after each execution?

For example:

QSqlQuery query;

query.prepare("INSERT INTO myTable (id, name) VALUES (:id, :name)");
query.bindValue(":id", id);
query.bindValue(":name", name);

if( !query.exec() )
{
    qDebug() << "Error";
}

query.finish() // or query.clear()?

query.prepare("INSERT INTO myProducts (product, price) VALUES (:product, :price)");
query.bindValue(":product", product);
query.bindValue(":price", price);

if( !query.exec() )
{
    qDebug() << "Error";
}

query.finish() // or query.clear()?

Note: I think I need to use the finish function, but I didn't understand exactly what the clear function does. The documentation says:

Clears the result set and releases any resources held by the query. Sets the query state to inactive.

1
  • 1
    What happens when you run it? Going off of memory, I used the same QSqlQuery just as you're doing, without doing finish() or clear(). My guess is things like lastError() and isValid() are probably erroneous w/o doing so. Commented Dec 13, 2016 at 17:35

1 Answer 1

3

There is no need to use any of these functions in your case, you can do just fine without any of the two lines you are asking about


Can I use the same QSQLQuery variable to execute multiple statements in Qt 5.3.2 using SQLite?

Of course you can, but you are not obligated to do so. For example, If you wanted to perform an SQL query and it happens that you already have a valid QSqlQuery object around that you are finished with (you are not willing to fetch any more data from it), you can just use that same QSqlQuery object with your new query as there is no need to create another QSqlQuery.

Should I call the finish or the clear function after each execution?

Here is what the docs say about 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.

This means that you only need to use it if you want to keep a QSqlQuery object that you are finished with for a long time, in order to use it. But there is really no need to do so, just let your object go out of scope when you are finished with it.

And about 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.

You can have a look at the Master Detail Example and in particular createConnection() function, to see how the same QSqlQuery object is used multiple times.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.