Which execute function should i use in MySQL connector/c++?

后端 未结 1 1764
渐次进展
渐次进展 2021-01-05 11:40

I need to write a wrapper (based on MySQL Connector/C++) which encapsulate specific SQL statements and provide user friendly interfaces, li

相关标签:
1条回答
  • 2021-01-05 12:36

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

    execute

    This function is the most generic one. It returns a boolean value, which 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'll want to use if you only want to use one to be as generic as possible.

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

    executeQuery

    This function directly returns a ResultSet which is useful for SELECT statements, and assumes there is indeed a result set to be returned.

    It is equivalent to call execute() followed by getResultSet().

    You'll want to use this function when you know you are using SQL code that returns results such as rows.

    executeUpdate

    This function returns an integer value which is useful for UPDATE statements and assumes there is an update count to be returned.

    It is equivalent to call execute() followed by getUpdateCount(), even though, for some reason, the return types are different (int vs uint64_t).

    This is the function to use when executing SQL statements that modify data and you need to know whether some data was modified.

    So,

    why there're so many execution functions rather than a simple and unified one?

    the unified one is in fact execute, which can be used to execute arbitrary SQL and handle the result appropriately, while the two other ones are convenient wrappers when you know what kind of query you execute.

    which execute function should i use for specify SQL statements?

    In your case, since you are writing a wrapper around the SQL language, each of your functions knows which kind of statement it will execute, so use of the convenience functions will allow you to write shorter code.

    For example:

    insert(), update(), delete() ---> executeUpdate()
    select()                     ---> executeQuery()
    
    0 讨论(0)
提交回复
热议问题