PDA

View Full Version : next() method of QSqlQuery



phoenix
3rd May 2007, 22:25
Hello all,

I was wondering how the next() method works. In the following peice of code, as per my understanding, when i say q.next() the pointer in the recordset object should point to second row and hence start from 2nd row and skip row 1. I have tested the code, it works perfectly but i am confused how?

Also say if the recordset object points to 0 initially and then increments to 1 when i call next(), wont the isNull(field) method always return false? since its pointing at zeroth entry and there is no data there?



if (!createConnection())
return 1;

QSqlQuery q("select * from person Order By firstname");
QSqlRecord myRecordSet = q.record();
int rec = myRecordSet.count();

qDebug("count is = %d", rec);
int nameCol = myRecordSet.indexOf("firstname");
while (q.next())
{
name = myRecordSet.fieldName(nameCol);
qDebug("names are:%s", q.value(nameCol).toString().toAscii().constData()) ;
}


Thanks in advance for any pointers for my better understanding.

--PH

codebehind
3rd May 2007, 22:47
when the SQL statement is processed the result is stored in a buffer.
the next() methods goes from first line to the last one by one each time you call it.

phoenix
3rd May 2007, 23:03
But what if i want to do a check to see if my recordset is actually valid or no before i browse through the recordset? and in that case i call the first() method before the next() method... and my pointer would be shifted n now i get data starting from 2nd row..?

thanks for your reply..

codebehind
5th May 2007, 17:44
But what if i want to do a check to see if my recordset is actually valid or no before i browse through the recordset?

I think there is a function like isValid()

when you call first() - the returned value is the first row. if you call next() it should return the second row.
for more detailed description read the manual (http://doc.trolltech.com/3.3/qsqlquery.html)