Search code examples
c++oraclesoci

How to avoid SOCI Error: Null value fetched and no indicator defined


I'm getting the "Error: Null value fetched and no indicator defined" when my application fetches the data from Oracle using SOCI.

How I can avoid it?

try
    {
        statement st = (sql.prepare <<
            "SELECT COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5, COLUMN6 FROM MY_TABLE"
                into(column_value1),
                into(column_value2),
                into(column_value3),
                into(column_value4),
                into(column_value5),
                into(column_value6)
                );
        st.execute();
        while (st.fetch())
        {
            cout << column_value1.c_str() << " : " << endl;
        }
    }

Solution

  • From the documentation (http://soci.sourceforge.net/doc/3.2/), an indicator needs to be provided to check whether the queried columns are null or correctly read.

    string phone;
    indicator ind;
    sql << "select phone from phonebook where name = :name",
        into(phone, ind), use(name);
    
    if (ind == i_ok)
    {
        cout << "The phone number is " << phone << '\n';
    }
    else
    {
        cout << "There is no phone for " << name << '\n';
    }
    

    Indicator has following values.

    // the enum type for indicator variables
    enum indicator { i_ok, i_null, i_truncated };