Richardson Ansong
Richardson Ansong

Reputation: 790

How to get total number of rows/records in an sqlite table

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?

Upvotes: 1

Views: 4807

Answers (2)

CL.
CL.

Reputation: 180300

The query returns a single column and a single row.

Just read that value:

query.first();
count = query.value(0).toInt();

Upvotes: 6

ahmed
ahmed

Reputation: 5590

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

Upvotes: -1

Related Questions