0

I am unable to run Select query using QODBC (don't need QMysql). They throw QSqlQuery::value: not positioned on a valid record. However, other queries run fine.

There are other threads on the same issue on Stackoverflow. But none of them solves my problem. Here are a few

QSqlQuery not positioned on a valid record

Select query returns "value: not positioned on a valid record" in Qt

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("Driver={MySQL ODBC 8.0 UNICODE Driver};DATABASE=dbname;");
db.setUserName("root");
db.setPassword("mysql");
if (!db.open()) {
    qDebug() << db.lastError();
} else {

    QSqlQuery query;
    query.prepare("SELECT id, name FROM users where id=1;");
    if (!query.exec())
    {
        qDebug() << "SQL error: "<< query.lastError().text() << endl;
    }
    query.first();

    qDebug() << query.value("name").toString() ;

}

Help appreciated

Chilarai
  • 1,842
  • 2
  • 15
  • 33
  • Your query result is empty. check for `query.first()` as `true` before you access `query.value("name")` – Mohammad Kanan Sep 13 '19 at 00:46
  • Yes after putting an ```if condition``` i get an error ```QSqlError("0", "QODBC3: Unable to fetch first", "[iODBC][Driver Manager]Optional feature not implemented")``` I am using a Mac. How do I resolve this? – Chilarai Sep 13 '19 at 02:35
  • Check Qt documentation for _QsqlQuery_ `prepare()` .. the issue could be there, with your specific OS/ ODBC driver . You can also test directly `query.exec("SELECT id, name FROM users where id=1;");` – Mohammad Kanan Sep 13 '19 at 03:56
  • Nope. It doesn't work. I skipped the prepare() statement and put the statement inside exec(). Any other way out please – Chilarai Sep 15 '19 at 04:44

0 Answers0