Counting records in a QSqLite db
-
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
Hi,int recCount = query_fix.prepare("SELECT COUNT(*) FROM Items");
query_fix.exec();@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;
-
@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;
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
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();
} -
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.