HiveBrain v1.2.0
Get Started
← Back to all entries
principlesqlMinor

execute() vs executeQuery() in MySQL Connector C++

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
connectorexecutequerymysqlexecute

Problem

With MySQL Connector C++, I can use the sql::Statement::execute() function if I am going to modify the database (INSERT, UPDATE, DELETE, MERGE), or the sql::Statement::executeQuery() function if I am going to do a simple read only query.

Supposing I do not know what is in the query, how can I choose what to do?

At the moment I just check if in the query string there are any of the INSERT, UPDATE, DELETE or MERGE tokens. But that is a far from perfect way to do it.

Is there another method to do it?

Should I just ignore the warning messages returned by MySQL when I use executeQuery() to modify the DB?

Thank you.

Solution

Each one of the execute calls is optimized in a different way depending based on their precise usage - for instance the executeUpdate method is optimized the way it is because it expects you to not return a result set from your call. You should choose the one that best suits your needs. The below is from the Connector C++ reference:


If you don't know ahead of time whether the SQL statement will be a SELECT or an INSERT, UPDATE, or DELETE, use the execute method. execute() returns true if the SQL query was a SELECT, and returns false if the statement was an INSERT, UPDATE, or DELETE. If the statement was a SELECT query, you can retrieve the results by calling the getResultSet method on the Statement instance. If the statement was an INSERT, UPDATE, or DELETE statement, you can retrieve the count of affected rows by calling getUpdateCount().

The article I mentioned can be found here:

http://dev.mysql.com/tech-resources/articles/mysql-connector-cpp.html

I hope this helps you.

Context

StackExchange Database Administrators Q#24542, answer score: 4

Revisions (0)

No revisions yet.