Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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 Sep 20 '12 at 8:36
    
It shouldn't perform any actions other than the ones you are asking it to do yourself. –  Mr.Brownstone Sep 20 '12 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 Sep 20 '12 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. –  Mr.Brownstone Sep 20 '12 at 9:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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