Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    General and Desktop
    1
    3
    871
    Loading More Posts
    • 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
      ashishbansal last edited by

      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 Reply Quote 0
      • A
        ashishbansal last edited by

        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 Reply Quote 0
        • A
          ashishbansal last edited by

          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 Reply Quote 0
          • First post
            Last post