2

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

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

5 Comments

When doing that i get QSqlQuery::value: not positioned on a valid record
Did you call query.exec() and query.first()
executed the query yea, but didnt put first, will try it now
Thank you :), having read that first() will position itself on the first valid record that makes perfect sense cheers
will probably change that variable name though dont want to typo that!
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.