0

I'm making a query using POCO::Data with ODBC Connector, and I need to check for NULL values from code. In the documentation, the Recordset object is supposed to have the function isNull, but the code I download here does not contains such a method (version 1.4.6p4 right now).

How can I check then if a value is NULL with POCO library? I'm trying to create a JSON string with the data retrieved from database.

My actual code looks like this:

Session session(bdName, conn);
Statement select(session);  

select << sSQL;  
select.execute();

RecordSet rs(select);
bool more = rs.moveFirst();
std::size_t cols = rs.columnCount();

sResult = "{\"rowsField\":[";

while (more) {
    if (sResult.back() != '[') sResult += ','; // Not first time
    sResult += "{\"columnsField\":[";

    for (std::size_t col = 0; col < cols; ++col) {
        std::string cName = rs.columnName (col);
        std::string tName = getPocoTypeName(rs.columnType(col));
        std::string val = "";

        if (!rs[col] || rs.value(col).isEmpty()) 
            val = "NULL"; // DOES NOT WORK
        else
            val = rs[col].convert<std::string>();

        if (col != 0) sResult += ',';
        sResult += "\n{\"nameField\":\"" + cName + '\"';
        sResult += ",\"typeField\":\"" + tName + '\"';
        sResult += ",\"valueField\":\"" + val + '\"';

        sResult += "}"; // each JSON column/value
    }
    sResult += "]}\n"; // columnsField (one per row)

    more = rs.moveNext();
}

3 Answers 3

1

Use Nullable<std::string> val = std::string("");

isNull is a member of Nullable, so you will be able to check if the return value is null..

Sign up to request clarification or add additional context in comments.

Comments

0

I know this is an old question but I have had issues with it. I am also formatting as JSON strings. You can use Poco::Recordset::IsNull(ColumnName) to check if a given column name's value is NULL or not.

The below code will replace NULLS with the empty string to prevent throwing errors:

Poco::Data::Statement statement(session);
statement << query;
statement.execute();
Poco::Data::RecordSet record_set(statement);

bool more = record_set.moveFirst();

while (more)
{
    std::map<std::string, std::string> row;

    for (std::size_t i = 0; i < record_set.columnCount(); ++i)
    {
         std::string val = "";
         if(!record_set.isNull(record_set.columnName(i)))  
             val = record_set[i].convert<std::string>(); 
           // print results for testing
         cout << "->" << record_set.columnName(i) << ":{" << val << "}";
    }

    ret.push_back(row);
    more = record_set.moveNext();
}

Comments

0

Use record_set[col].isEmpty() for checking NULL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.