Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QSqlQuery not positioned on a valid record

Tags:

c++

mysql

qt

I'm trying select a field of my date base, the code is:

if (db.db().isOpen())
{
  qDebug() << "OK";
  QSqlQuery query("SELECT state FROM jobs WHERE jobId = '553'", db.db());
  qDebug() << query.value(0).toString();
}
else
  qDebug() << "No ok"; 

The query is correct because when I do qDebug() << query.size;, it returns 1.

but with qDebug() << query.value(0).toString();, it returns:

OK 
QSqlQuery::value: not positioned on a valid record
""

How can I fix it?

like image 674
Jjreina Avatar asked Jan 25 '12 09:01

Jjreina


1 Answers

You should call query.first() before you can access returned data. additionally if your query returns more than one row, you should iterate via query.next().

like image 121
Davita Avatar answered Oct 22 '22 09:10

Davita