QSqlQuery how to get value of a count?

对着背影说爱祢 提交于 2019-12-24 03:48:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!