Important: Please read the Qt Code of Conduct -

[Solved] error while inserting row in table (QSql)

  • Hello everyone,

    I am getting error while trying to insert row into table.
    Code for creating table :
    QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
    QSqlQuery qry(mydb);
    QString qryStr = "create table if not exists downloadList(id integer, filename varchar)";
    qDebug() << "Unable to create table!";
    qDebug() << qry.lastError();
    qDebug() << "Table successfully created";
    Output : Table Successfully created
    But while inserting row,
    Code For Inserting row in table:
    QSqlQuery qry(mydb);
    QString qryStr = "insert into downloadList (id, filename) values (:id, :filename)";
    qry.bindValue(":id", 1);
    qry.bindValue(":filename", "foo");
    qDebug() << "Unable to insert row in table!";
    qDebug() << qry.lastError();
    Unable to insert row in table!
    QSqlError("", "Parameter count mismatch", "")

    Parameter count is correct then why I am getting this error? :/

    Ashish Bansal

  • Am I really doing some very silly mistake?
    Anyone with any suggestion or something which can help me in debugging it?

  • Ah, after several hours of banging head with wall, It finally got solved!
    My mistake was that I was passing parameter to QSqlQuery.
    I don't have to pass string parameter to QSqlQuery::exec after preparing query using QSqlQuery::prepare.

Log in to reply