Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] error while inserting row in table (QSql)
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ashishbansal
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ashishbansal
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ashishbansal
        wrote on last edited by
        #3

        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!!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved