且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我应该在MySQL Connector/C ++中使用哪个执行功能?

更新时间:2023-11-23 15:17:40

这三个函数中的每一个都有特定的用途,可以从它们的返回类型中猜出.

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

此功能是最通用的功能.它返回一个布尔值,如果查询返回多个结果,则该值为true;如果查询不返回任何内容或更新计数,则为false.

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.

如果返回true,则需要使用ResultSet * getResultSet()来获取结果.
如果返回false,则需要使用uint64_t getUpdateCount()来获取更新的行数.

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.

此函数直接返回ResultSet,这对SELECT语句很有用,并假定确实存在要返回的结果集.

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

等效于先调用execute(),再调用getResultSet().

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

当您知道正在使用返回行等结果的SQL代码时,将要使用此功能.

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

此函数返回一个整数值,该值对UPDATE语句很有用,并假定有要返回的更新计数.

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

即使由于某种原因返回类型不同(int vs uint64_t),它也等效于先调用execute(),再调用getUpdateCount().

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

这是执行修改数据的SQL语句时要使用的功能,您需要知道是否已修改某些数据.

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

所以

为什么执行功能这么多,而不是简单统一的功能呢?

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

统一的实际上是execute,可用于执行任意SQL并适当地处理结果,而另外两个则是方便的包装器,当您知道要执行哪种查询时.

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.

我应该使用哪个执行函数来指定SQL语句?

which execute function should i use for specify SQL statements?

在您的情况下,由于您正在编写围绕SQL语言的包装器,因此每个函数都知道它将执行哪种语句,因此使用便捷函数将使您可以编写较短的代码.

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.

例如:

insert(), update(), delete() ---> executeUpdate()
select()                     ---> executeQuery()