PDA

View Full Version : QSqlQuery and seek() - doesn't work on first call.



amicitas
2nd October 2008, 14:55
I am trying to use the QSqlQuery.seek() function. I am running into a problem in which the first time that I call seek after a query it doesn't seem to do anything. If I call it a second time it does what I would expect. If I call next() before seek() it also behaves as expected. What is going on here?

I am using the SQLite database for this and programing in PyQt4.

Example Code:


print 'is_active', self.query.isActive()
print 'is_select', self.query.isSelect()
print 'is_valid', self.query.isValid()

print ''
print 'seek:', self.query.seek(0)
print 'is_valid', self.query.isValid()

print ''
print 'seek:', self.query.seek(0)
print 'is_valid', self.query.isValid()

print ''
print 'next', self.query.next()
print 'is_valid', self.query.isValid()

print ''
print 'seek:', self.query.seek(0)
print 'is_valid', self.query.isValid()


Result:


is_active True
is_select True
is_valid False

seek: False
is_valid False

seek: True
is_valid True

next False
is_valid False

seek: True
is_valid True


Thanks for any help with this,

amicitas

yuriry
2nd October 2008, 18:25
I tested your code on PostgreSQL using C++ and got correct results (I used a query that returns a single record, looks like you did the same). May be there is a bug in SqlLite driver or in PyQt4?

Test program


int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setDatabaseName("pm");

bool ok = db.open();
if(! ok) {
qDebug() << db.lastError().text();
return 1;
}

QSqlQuery query;
if(query.exec("select * from pm.portfolio where id = 1")) {
qDebug() << "is_active" << query.isActive();
qDebug() << "is_select" << query.isSelect();
qDebug() << "is_valid" << query.isValid();

qDebug() << "";
qDebug() << "seek:" << query.seek(0);
qDebug() << "is_valid" << query.isValid();

qDebug() << "";
qDebug() << "seek:" << query.seek(0);
qDebug() << "is_valid" << query.isValid();

qDebug() << "";
qDebug() << "next" << query.next();
qDebug() << "is_valid" << query.isValid();

qDebug() << "";
qDebug() << "seek:" << query.seek(0);
qDebug() << "is_valid" << query.isValid();
}
else {
qDebug() << query.lastError();
}
}


Result


is_active true
is_select true
is_valid false

seek: true
is_valid true

seek: true
is_valid true

next false
is_valid false

seek: true
is_valid true