在下面的代码中,目的是检查null,但代码不断抛出异常。
pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");
res = pstmt->executeQuery();
res->next();
if(res->isNull(1))
{
cout << " In null";
}
else
{
cout << " Is not Null";
}
如何检查null或空结果集?
答案 0 :(得分:1)
这是如何从数据库中获取数据的示例:
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;
用于获取结果集的API对于(简单)语句是相同的 准备好的陈述。如果您的查询返回一个结果集,请使用 sql :: Statement :: executeQuery()或 sql :: PreparedStatement :: executeQuery()来运行您的查询。两种方法 返回sql :: ResultSet对象。预览版本确实缓冲所有 客户端上的结果集以支持游标。
了解更多mysql reference
换句话说,使用executeQuery()返回的sql :: ResultSet中的next()函数或previous()函数。