Search code examples
c++qtqsqlquery

QSqlQuery how to get value of a count?


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


Solution

  • 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