MySQL 连接器/C++ 1.1 开发人员指南 / 第 6 章连接器/C++:使用示例 /
6.3 获取结果
用于获取结果集的 API 对于(简单)语句和准备好的语句是相同的。如果您的查询返回一个结果集,请使用
sql::Statement::executeQuery()
或
sql::PreparedStatement::executeQuery()
运行您的查询。两种方法都返回
sql::ResultSet
对象。默认情况下,Connector/C++ 缓冲客户端上的所有结果集以支持游标。
Press CTRL+C to copy// ... sql::Connection *con; sql::Statement *stmt; sql::ResultSet *res; // ... stmt = con->createStatement(); // ... res = stmt->executeQuery("SELECT id, label FROM test ORDER BY id ASC"); while (res->next()) { // You can use either numeric offsets... cout << "id = " << res->getInt(1); // getInt(1) returns the first column // ... or column names for accessing results. // The latter is recommended. cout << ", label = '" << res->getString("label") << "'" << endl; } delete res; delete stmt; delete con;
笔记
在前面的代码片段中,列索引从 1 开始。
笔记
您必须使用 显式释放sql::Statement
、
sql::Connection
和
sql::ResultSet
对象
delete
。
下载包中包含的示例演示了游标的用法。