3

I'm trying to insert data into a QtSql database, but I keep getting the error:

"Parameter count mismatch"

What could be what I'm doing wrong?

#include <QtSql>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qDebug() << QSqlDatabase::drivers();

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("LOCALHOST");
    db.setDatabaseName("people");
    db.setUserName("root");
    db.setPassword("");

    if(db.open()) {
        qDebug() << "Opened!";

        QSqlQuery query;

        query.prepare("INSERT INTO person (id, forename, surname) "
                      "VALUES (:id, :forename, :surname)");
        query.bindValue(":id", 1001);
        query.bindValue(":forename", "Bart");
        query.bindValue(":surname", "Simpson");
        query.exec();

        if( !query.exec() )
            qDebug() << query.lastError().text();
        else
            qDebug( "Inserted!" );

        db.close();

    } else {
        qDebug() << "Not opened";
    }
}
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142
  • 2
    Thanks for posting a nice self-contained test case. You could save three lines by: 1. Not including `` and ``: the `` module includes the `` dependency. 2. Not calling `a.exec()` as you have synchronous code. You can remove the return statement in `main` safely here. – Kuba hasn't forgotten Monica Nov 16 '16 at 17:45

1 Answers1

3

You don't have a table named person in the database. You are trying to insert values into a table that does not exist.


I think that the error message is wrong. But anyway, I added a CREATE statement to your code, so that the table is created before the INSERT statement is executed:

#include <QtSql>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(":memory:");

    if(!db.open()){
        qDebug() << "Not opened";
        return 1;
    }
    qDebug() << "Opened!";

    QSqlQuery query;

    //CREATE the table before executing the INSERT statement
    query.exec("CREATE TABLE person (id INTEGER, forename TEXT, surname TEXT);");

    query.prepare("INSERT INTO person (id, forename, surname) "
                  "VALUES (:id, :forename, :surname)");
    query.bindValue(":id", 1001);
    query.bindValue(":forename", "Bart");
    query.bindValue(":surname", "Simpson");

    if( !query.exec() )
        qDebug() << query.lastError().text();
    else
        qDebug( "Inserted!" );

    return 0;
}

This prints Inserted! now. But if I comment out the CREATE statement line I get the same error in your question:

" Parameter count mismatch"
Mike
  • 8,055
  • 1
  • 30
  • 44
  • 2
    An equivalent case happens when trying top open a SQLite database file with `QSqlDatabase::open` but it has vanished from disk. It will then be silently re-created as an empty database file, resulting in `" Parameter count mismatch"` on the first query because there are no tables inside. Means, always check that the expected tables are present when opening SQLite databases in Qt. – tanius Jun 21 '20 at 12:47