I am trying to get a bigint
value (named "NUM" in my example) that comes from my database (Microsoft SQL Server). In the following code, I am trying to get it as an int
even if I know that an int
is smaller than a bigint
but the cast always fails. I also tried to get it as a long long int
and as some other types of variable but I am always getting the same problem at runtime as shown below.
try
{
session sql(odbc, "...");
rowset<row> rs = (sql.prepare << "SELECT MAX(NUM) AS MAX_NUM FROM T1");
for (rowset<row>::const_iterator it = rs.begin(); it != rs.end(); ++it)
{
row const& row = *it;
cout << "MAX_NUM: " << row.get<int>("MAX_NUM") << endl;
}
}
catch (exception const &e)
{
cerr << "Error: " << e.what() << '\n';
}
I have also tried the following getters:
cout << "MAX_NUM: " << row.get<long>("MAX_NUM") << endl;
cout << "MAX_NUM: " << row.get<long long>("MAX_NUM") << endl;
cout << "MAX_NUM: " << row.get<long long int>("MAX_NUM") << endl;
All my tests print the same error when I run my application:
Error: std::bad_cast
My question is : What is the correct way to get this value ?