问题
I have a query that is just getting the count of table based on some criteria eg:
QSqlQuery query("SELECT COUNT(*) FROM some_table WHERE some_value = :something");
query.bindValue(":something", "something");
my question is how do i get result of this back, obviously this is where you would use
query.value(int);
query.value(QString);
But the docs say that int index one shouldnt be used because we dont know what the index would be andi cant use the string one because there is no filed to define as the one i want the result for. so assuming i am correct in saying that how is it that i can get the count integer value into a int variable from this query
cheers
回答1:
Make an alias to field count. Like this:
QSqlQuery query("SELECT COUNT(*) CNT FROM some_table WHERE some_value = :something");
and then query by name CNT
回答2:
You can also use: Query.next(); Query.value(0).toInt();
For this msg: QSqlQuery::value: not positioned on a valid record you should do: Query.next(); to change the pointer to first record!
来源:https://stackoverflow.com/questions/26039000/qsqlquery-how-to-get-value-of-a-count