0

Why query.next() returns false, when query.isSelected() and query.isActive() return true. And datas for column numbers and index of EventNote are correct.

Here is the code:

QVector<QString> DataBase::GetEventNote(int eventId)
 {

        QSqlQuery query;
        query.prepare("SELECT * FROM Events WHERE idEvent=(?)");
        query.addBindValue("eventId");

    if(!query.exec())       // -> returns true
    {
        QMessageBox mb;
        mb.setText(query.lastError().text());
        mb.setWindowIcon(QIcon("icon.png"));
        mb.exec();
    }
    QVector<QString> debug1;
    bool v = query.isValid(); // -> returns false
    bool s = query.isSelect(); // -> returns true
    bool a = query.isActive(); // -> returns true
    bool b = query.first(); // -> returns false
    bool l = query.last(); // -> returns false
    bool p = query.previous(); // -> returns false
    QSqlRecord sr = query.record();
    int brsr = sr.count(); // -> returns correct number of columns
    QString str = query.lastQuery();

    const QSqlResult *r =query.result();

    int nameCol = sr.indexOf("EventNote"); // index of the field "EventNote" is correct (index 5)

    while (query.next()) // -> returns false
    {
        QString debug2 =  query.value(nameCol).toString(); // output all EventNote
        debug1.push_back(debug2);
    }


    return debug1;
}
Alexander
  • 3,129
  • 2
  • 19
  • 33

1 Answers1

0

It seems that there is a problem with your database.

bool QSqlQuery::isValid () const

Returns true if the query is currently positioned on a valid record; otherwise returns false.

This enforced by the fact that:

query.first()
query.last()

Retrieves the last record in the result, if available, and positions the query on the retrieved record. {...] Returns true if successful. If unsuccessful the query position is set to an invalid position and false is returned.

So by calling those methods you are actually moving the iterator in the results (which are not a on valid record anyways), same with query.previous().

Kirell
  • 9,228
  • 4
  • 46
  • 61
  • But why then : `QSqlRecord sr = query.record(); int brsr = sr.count(); // -> returns correct number of columns` returns correct number of columns ? – user2944505 Feb 28 '14 at 10:24
  • This is strange, did you try to remove the query,first(), last, etc. and to do a plain simple query, without binding a value ? – Kirell Feb 28 '14 at 10:36
  • if i remove query.first(), last()... i's the same, query.next() return false, and if i don't bind value quary.exec() then return false. This is really strange, cos i use this so many times and there weren't any problems – user2944505 Feb 28 '14 at 10:50
  • Are you sure you have read access to the database, sometimes if you open it with an external app you can't. Or maybe another instance of your program is launched in background ? – Kirell Feb 28 '14 at 15:02