我应该在MySQL Connector/C ++中使用哪个执行功能? [英] Which execute function should i use in MySQL connector/c++?

查看:226
本文介绍了我应该在MySQL Connector/C ++中使用哪个执行功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个包装器(基于 MySQL Connector/C ++ ),该包装器封装了特定的SQL语句并提供了用户友好的界面,例如: insert() 更新()删除()选择()

I need to write a wrapper (based on MySQL Connector/C++) which encapsulate specific SQL statements and provide user friendly interfaces, like: insert(), update(), delete(), select(), etc.

在MySQL的本机C API中,每个SQL语句都可以通过简单地调用"mysql_real_query()" 来执行,但是现在,在 MySQL Connector/C ++ 中,有点困惑.在 sql :: Statment 类中有 3 个执行函数,在 sql :: PreparedStatement 类中有 6 个执行函数:

In MySQL's native C API, every SQL statement could be executed by simply call "mysql_real_query()", but now, in MySQL Connector/C++, things get a little confused. There's 3 execute functions in sql::Statment class and 6 execute functions in sql::PreparedStatement class:

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

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:

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);

我想知道为什么执行功能如此之多,而不是一个简单而统一的功能?我应该使用哪个执行函数来指定SQL语句?

注意:我使用的是 MySQL Connector/C ++ 1.1.5 ,并以 MySQL Server 5.6 作为后端.

Note: I'm using MySQL Connector/C++ 1.1.5 with MySQL Server 5.6 as backend.

推荐答案

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

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()

这篇关于我应该在MySQL Connector/C ++中使用哪个执行功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆