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.


1 Answer 1


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:


I hope this helps you.

  • OK, that works. Do you know if it is possible to know if any write operation will be performed before running the query itself? I need it in case I want to access the DB in read only mode.
    – Pietro
    Commented Sep 20, 2012 at 8:36
  • It shouldn't perform any actions other than the ones you are asking it to do yourself. Commented Sep 20, 2012 at 8:40
  • Yes, but suppose I have a "readOnly" flag in my program. If it is true, I am forbidden to make any change to the DB. So I would like to know if the current query can modify anything before running it.
    – Pietro
    Commented Sep 20, 2012 at 8:52
  • 1
    You could add a transaction to your connection, check to see if false is returned from the execute call and then rollback. Alternatively you need to handle the checking yourself - it will not do it for you. Commented Sep 20, 2012 at 9:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.