QSqlQuery未定位在有效记录上

时间:2022-06-01 20:44:25

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.

查询是正确的,因为当我执行qDebug()<< query.size;时,它返回1。

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

但是使用qDebug()<< query.value(0).toString();,它返回:

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

How can I fix it?

我该如何解决?

1 个解决方案

#1


20  

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().

您应该在可以访问返回的数据之前调用query.first()。另外,如果您的查询返回多行,则应通过query.next()进行迭代。

#1


20  

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().

您应该在可以访问返回的数据之前调用query.first()。另外,如果您的查询返回多行,则应通过query.next()进行迭代。