Re: [Cppcms-users] [cppdb] Get the count after select count(*)
Brought to you by:
artyom-beilis
|
From: Artyom B. <art...@ya...> - 2011-07-27 14:47:34
|
> From: Allan SIMON <all...@su...>
>
> Hello,
>
> I want to retrieve the number of row in my table users, so I wrote the
> following
>
> getUsersCount = sqliteDb.create_prepared_statement(
> "SELECT count(*) as total FROM users "
> );
>
> int total = getUsersCount.query().get<int>("total");
>
> But I run it, i get the following exception
>
> "2011-07-27 13:20:41 GMT; cppcms, error: Caught exception
> [cppdb::null_value_fetch attempt fetch null column]"
>
> (which is different from the exception i get if i try to get the value
> of a column that does not exists at all)
>
>
> Am I doing something wrong ?
>
Yes.
When you call query() you get a result set that you should
iterate with next() functions. You looking for row()
that would basically call query() and then call next()
So rewrite it as:
int total = getUsersCount.row().get<int>("total");
See: http://cppcms.sourceforge.net/sql/cppdb/classcppdb_1_1statement.html#7fd1167898cef7a5592bfd00453ab2f7
By the way: session::prepare() by default does what create_prepared_statement() does
And the code above can be rewritten nicely as
sqliteDb <<
"SELECT count(*) "
"FROM users " << cppdb::row
>> total;
Artyom
|