1

i want to know how to get the total number of rows/records in an sqlite table and assign it to a variable. I did this;

    QSqlQuery query("SELECT COUNT(*) FROM class1_bills");

But the problem is, how do i assign the result to a variable? I tried;

int rows = query.seek(0);

and;

int rows = query.value(0).toInt();

Yes, I know doing that accesses just the record at field position 0 in the table. But it seems Qt's query methods are for accessing particular records at field positions only. If i'm wrong please correct me. So how do i get the total row count in the table?

1

2 Answers 2

6

The query returns a single column and a single row.

Just read that value:

query.first();
count = query.value(0).toInt();
Sign up to request clarification or add additional context in comments.

1 Comment

If you pass a query to QSqlQuery constructor it will be executed, no need to call exec()
-1

It's simple, just like any other query:

int rows = 0;
QSqlQuery query("SELECT COUNT(*) FROM class1_bills");
if (query.next()) {
    rows = query.value(0).toInt();
}
// Do something with rows

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.