Search code examples
qtqtsql

Why does parameter count mismatch?


From what I gather it's a mistake in your database/query and I've tried the following in DB Browser for Sqlite and it works:

    UPDATE odgovori SET odgovor="1131" WHERE korisnik_ID="2" AND pitanje_ID="1";
    INSERT INTO odgovori (korisnik_ID, pitanje_ID, odgovor) SELECT "2", "1", "1" WHERE (SELECT Changes()=0);

but, when I try this in QT:

    Q_ASSERT(qry.driver()->hasFeature(QSqlDriver::NamedPlaceholders));
    qry.prepare("UPDATE odgovori SET odgovor=:odgovor WHERE korisnik_ID=:korisnik_ID AND pitanje_ID=:pitanje_ID;"
    "INSERT INTO odgovori (korisnik_ID, pitanje_ID, odgovor) SELECT :korisnik_ID, :pitanje_ID, :odgovor WHERE (SELECT Changes()=0);");
    qry.bindValue(":odgovor", odgovor);
    qry.bindValue(":korisnik_ID", id);
    qry.bindValue(":pitanje_ID", QString::number(pitanjeid)); 
    qry.exec();
    qDebug()<<qry.lastError();

I get QSqlError("", "Parameter count mismatch", ""). I've tried with ? and qry.addBindValue and I get the same result. What am I missing?


Solution

  • As pointed out by @wthung in the comments, I tried manually inserting fixed numbers and the debugger throws QSqlError("21", "Unable to execute multiple statements at a time", "not an error")
    So I broke the qry into two parts and it worked.

    qry.prepare("UPDATE odgovori SET odgovor=:odgovor WHERE korisnik_ID=:korisnik_ID AND pitanje_ID=:pitanje_ID;");
    qry.bindValue(":odgovor", odgovor);
    qry.bindValue(":korisnik_ID", id);
    qry.bindValue(":pitanje_ID", QString::number(pitanjeid));
    qry.exec();
    qry.prepare("INSERT INTO odgovori (korisnik_ID, pitanje_ID, odgovor) SELECT :korisnik_ID, :pitanje_ID, :odgovor WHERE (SELECT Changes()=0);");
    qry.bindValue(":odgovor", odgovor);
    qry.bindValue(":korisnik_ID", id);
    qry.bindValue(":pitanje_ID", QString::number(pitanjeid));
    qry.exec();