What execution function should I use in MySQL Connector / C ++?

I need to write a shell (based on MySQL Connector / C ++ ) that encapsulates certain SQL statements and provides user-friendly interfaces, for example: insert () , update () , delete () , select () , etc.

In the MySQL user C API, each SQL statement can be executed with a simple call to mysql_real_query () , but now, in MySQL Connector / C ++ , everything is a little confused. There 3 perform functions in sql :: Statment and 6 perform functions in the sql :: PreparedStatement class:

mysql-connector-C ++ - 1.1.5 \ driver \ mysql_statement.h:

bool execute(const sql::SQLString& sql); sql::ResultSet* executeQuery(const sql::SQLString& sql); int executeUpdate(const sql::SQLString& sql); 

mysql-connector-C ++ - 1.1.5 \ driver \ mysql_prepared_statement.h, 104:

 bool execute(); bool execute(const sql::SQLString& sql); sql::ResultSet executeQuery(); sql::ResultSet executeQuery(const sql::SQLString& sql); int executeUpdate(); int executeUpdate(const sql::SQLString& sql); 

I wondered why there are so many executable functions, and not just unified? And which one performs the function should I use to specify SQL statements?

Note. I am using MySQL Connector / C ++ 1.1.5 with MySQL Server 5.6 as a backend.

+5
source share
1 answer

Each of the three functions has a specific application, which can be guessed from their return type.

Execute

This feature is the most common. It returns a boolean whose value is true if the query returns multiple results, or false if the query returns either nothing or an update count.

This is the function you want to use if you want to use it as general as possible.

If it returns true, you want to use ResultSet * getResultSet() to get the results.
If it returns false, you want to use uint64_t getUpdateCount() to get the number of rows updated.

ExecuteQuery

This function directly returns a ResultSet , which is useful for SELECT , and assumes that there really is a return result.

This is equivalent to calling execute() , followed by getResultSet() .

You want to use this function when you know that you are using SQL code that returns results, such as strings.

executeUpdate

This function returns an integer value, which is useful for UPDATE and assumes that the number of updates will be returned.

This is equivalent to calling execute() , followed by getUpdateCount() , although for some reason the return types are different (int vs uint64_t).

This is the function used to execute SQL statements that modify data, and you need to know if some data has been modified.

So,

why are there so many execution functions and not simple and unified?

actually unified execute , which can be used to execute arbitrary SQL and properly process the result, while the other two are convenient shells when you know which query you are doing.

which performs the function should I use to specify SQL statements?

In your case, since you are writing a wrapper around the SQL language, each of your functions knows what kind of execution will be performed, so using convenient functions will allow you to write shorter code.

For instance:

 insert(), update(), delete() ---> executeUpdate() select() ---> executeQuery() 
+11
source

Source: https://habr.com/ru/post/1209072/


All Articles