[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");
    mydb.setDatabaseName("dmDownloadList");
    QSqlQuery qry(mydb);
    QString qryStr = "create table if not exists downloadList(id integer, filename varchar)";
    if(!qry.exec(qryStr)){
    qDebug() << "Unable to create table!";
    qDebug() << qry.lastError();
    }else{
    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.prepare(qryStr);
    qry.bindValue(":id", 1);
    qry.bindValue(":filename", "foo");
    if(!qry.exec(qryStr)){
    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? :/

    Regards,
    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.
    Huh!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.