Greaterme
Greaterme

Reputation: 41

How does one check for null or an empty result set with MySQL C++ connector?

In the following code, the intention is to check for null,but the code keeps throwing exception.

 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";
 }

How does one check for null or an empty result set?

Upvotes: 1

Views: 2454

Answers (1)

user5106417
user5106417

Reputation:

This is an example of how to fetch data from the database:

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;

The API for fetching result sets is identical for (simple) statements and prepared statements. If your query returns one result set, use sql::Statement::executeQuery() or sql::PreparedStatement::executeQuery() to run your query. Both methods return sql::ResultSet objects. The preview version does buffer all result sets on the client to support cursors.

Read more mysql reference

In other words, use either the next() function or previous() function in the sql::ResultSet which is returned by executeQuery().

Upvotes: 2

Related Questions