Counting records in a QSqLite db
-
@gabor53
Hi,
try this query,
SELECT Count(*) FROM TableName; -
@Venkatesh-V
I tried like this:recCount = query_fix("SELECT COUNT(*) FROM Items");
and I got the following error message:
C:\Programming\Projects\Folkfriends_bzr\checkout\fixdb.cpp:121: error: no match for call to '(QSqlQuery) (const char [27])'
recCount = query_fix("SELECT COUNT(*) FROM Items");
^
Any idea why? -
Hi,
Because that's not how you use a QSqlQuery object.
A
select count
should be handled the same as classicselect
call. You'll just have one result which will contain the number of rows. -
@gabor53
Hi,int recCount = query_fix.prepare("SELECT COUNT(*) FROM Items");
query_fix.exec(); -
@gabor53 What is supposed to work ?
-
@Venkatesh-V
This returns 1, but there are 7 records in the db. Any idea why?int recNum; recNum = query_fix.prepare ("SELECT COUNT(*) FROM Items"); query_fix.exec (); qDebug() << "Record count in fixdb: " << recNum;
-
mrjj Lifetime Qt Championreplied to gabor53 on 10 Aug 2017, 13:26 last edited by mrjj 8 Oct 2017, 13:28
hi
If i show you how the function is defined, can you then guess why its 1 ?
bool QSqlQuery::prepare(const QString & query)I will give you a hint and say that
query_fix.exec ();
must be used.
Also the value of true is often 1. -
Hi,
Try this,int recNum =0;
query_fix.prepare ("SELECT * FROM Items");
if( query_fix.exec ()){
while( query.next() )
{
recNum ++;
}
}
qDebug() << "Record count in fixdb: " << recNum; -
Or you can also use this,
QSqlQuery q;
q.prepare("SELECT COUNT (*) FROM TableName");
q.exec();
int rows= 0;
if (q.next()) {
rows= q.value(0).toInt();
} -
@Venkatesh-V
This worked.
Thank you.
12/15