开发者

query.next() returning false

I have used the query.next() function inside my code, but its returning false. I even checked in the database, there are 4 records present. But the code shows only one. However if i use query.next() before the code of query.valid() then it doesn't show any record Please help

  qDebug() << "entering payment: get all the unchecked invoices for user: " + user;
  QStringList tmp;
  QSqlQuery query(m_storageUserManager->database());
  m_storageUserManager->dumpTable(m_invoiceInfoTable);
  m_storageUserManager->dumpTable(m_invoiceUserTable);
  qDebug()<<"THE NAME OF THE INVOICE USER TABLE_----=-----------------"<<m_invoiceInfoTable;
  qDebug()<<"THE NAME OF THE INVOICE USER TABLE_----=-----------------"<<m_invoiceUserTabl开发者_开发知识库e;
  query.prepare("SELECT invoice FROM "+ m_invoiceInfoTable +" WHERE invoice = (SELECT
                 invoice FROM "+ m_invoiceUserTable +" WHERE user=:user)");
//  query.prepare("SELECT invoice FROM " + m_invoiceInfoTable + ","+ m_invoiceUserTable +" WHERE " + m_invoiceInfoTable + ".user = " + m_invoiceUserTable + ".:user");
  query.bindValue(":user", user);
  query.exec();
  query.first();
  qDebug()<<"Unchecked invoices done!!! " ;
  if(query.isValid()) {
    do {
      tmp.append(query.value(0).toString());    //as the value returned by value() is a QVariant so we need to change it to String.
    } while(query.next());
  } else
    tmp.append("No Unchecked invoice in the database");
  return tmp;


To check if the query was successful you should test the return value of either QSqlQuery::exec() or QSqlQuery::isActive() before trying to call first/next/last (when you pass the query string to the constructor of QSqlQuery, the query is already executed, so, you need to use QSqlQuery::isActive()).

first(), next() and last() return true if they positioned the query on a valid record, you don't have to test isValid() separately. Since first() is a positioning function too, you can read the value without calling next() directly after, unless you want to skip the first record.

Since you may want to add fields from the the "invoice-info" table to your query, I kept the subquery (with IN instead of = as Mat already answered in the comment).

query.prepare(QString("SELECT invoice FROM %1 WHERE invoice "
                      "IN (SELECT invoice FROM %2 WHERE user=:user)")
              .arg(m_invoiceInfoTable, m_invoiceUserTable));    
/*
// Or with an inner join
query.prepare(QString("SELECT %1.invoice FROM %1 "
              "INNER JOIN %2 ON %1.invoice = %2.invoice "
              "WHERE user=:user").arg(m_invoiceInfoTable, m_invoiceUserTable));*/    
query.bindValue(":user", user);

if (!query.exec()) { 
    tmp.append("Query error: %1" + query.lastError().text());
} else if (!query.first()) { 
    tmp.append("No Unchecked invoice in the database");
} else {
    do {
        tmp.append(query.value(0).toString());
    } while(query.next());
} 


Try

query.prepare("SELECT invoice FROM " + m_invoiceInfoTable + " WHERE user=:user");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜