Results 1 to 4 of 4

Thread: Adding values into a table - "Parameter count mismatch" error in SQLite

  1. #1
    Join Date
    Dec 2014
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Adding values into a table - "Parameter count mismatch" error in SQLite

    Hi all,
    I have being trying to create a program to Add Users, Update and Delete Users. Update and Delete parts are working finely. But when I run the Add Users, it displays an error saying "Parameter count mismatch".
    your help is highly appreciated

    database table-
    CREATE TABLE "users" (
    "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
    "user_id" VARCHAR(20) ,
    "name" VARCHAR(50) NOT NULL,
    "reg_date" DATETIME DEFAULT CURRENT_DATE,
    "designation" VARCHAR(10) NOT NULL,
    "password" VARCHAR(20) NOT NULL
    );

    Code-

    void example::AddUser()
    {
    QString userName_s;
    userName_s=ui6->lineEdit->text();


    if(userName_s=="")
    QMessageBox::warning(this,tr("Warning"),tr("Invali d input !!!"));
    else
    {
    QString id_s=this->Increment();
    ui6->label_21->setText(id_s);

    QSqlQuery query;
    query.prepare("INSERT INTO users (user_id,name,reg_date,designation,password ) VALUES (?,?,?,?,?)");

    query.bindValue(":user_id = ", id_s );
    query.bindValue(":name", ui6->lineEdit->displayText());
    query.bindValue(":reg_date = ",QDate::currentDate().toString("dd-MM-yyyy") );
    query.bindValue(":designation", ui6->comboBox->currentText());
    query.bindValue(": password = ", id_s );
    query.exec();

    if (!query.isActive())//checks wether the connection is active
    {
    QMessageBox::warning(this, tr("Database Error"),
    query.lastError().text());
    }

    this->DisplayAddedUser();

    ui6->lineEdit->clear();
    ui6->label_21->clear();
    }
    }

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Adding values into a table - "Parameter count mismatch" error in SQLite

    Quote Originally Posted by Lale123 View Post
    Hi all,
    query.prepare("INSERT INTO users (user_id,name,reg_date,designation,password ) VALUES (?,?,?,?,?)");

    query.bindValue(":user_id = ", id_s );
    query.bindValue(":name", ui6->lineEdit->displayText());
    query.bindValue(":reg_date = ",QDate::currentDate().toString("dd-MM-yyyy") );
    query.bindValue(":designation", ui6->comboBox->currentText());
    query.bindValue(": password = ", id_s );
    query.exec();
    You are mixing named and unnamed query parameters. Either replace the question marks in your query string with the named parameters or leave the query ASIS and use the QSqlQuery::bindValue overload that uses the positional parameters.

    Also, please use [code] and [/code] when pasting code examples.
    Last edited by jefftee; 14th December 2014 at 06:37.

  3. The following user says thank you to jefftee for this useful post:

    Lale123 (15th December 2014)

  4. #3
    Join Date
    Dec 2014
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Adding values into a table - "Parameter count mismatch" error in SQLite

    Thanks for the kind help jthomps!!

  5. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Adding values into a table - "Parameter count mismatch" error in SQLite

    No problem, glad to help. I did want to mention that if you are going to use named query parameters, the bindValue is just the parameter name. In your example, you have " = " as part of the parameter name, it should be something like:

    Qt Code:
    1. query.prepare("INSERT INTO users (user_id,name,reg_date,designation,password ) VALUES (:user_id,:name,:reg_date,:designation,:password)");
    2.  
    3. query.bindValue(":user_id", id_s );
    4. query.bindValue(":name", ui6->lineEdit->displayText());
    5. query.bindValue(":reg_date",QDate::currentDate().toString("dd-MM-yyyy") );
    6. query.bindValue(":designation", ui6->comboBox->currentText());
    7. query.bindValue(":password", id_s );
    8. query.exec();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 14
    Last Post: 16th May 2017, 04:51
  2. QT5 and SQLLite: Parameter count mismatch upon Insert
    By skruffynerherder in forum Qt Programming
    Replies: 9
    Last Post: 10th October 2014, 02:34
  3. Replies: 6
    Last Post: 13th June 2014, 07:54
  4. Sql problem - parameter mismatch count
    By Marina K. in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2011, 19:27
  5. Parameter count mismatch in create table statement
    By croscato in forum Qt Programming
    Replies: 5
    Last Post: 4th February 2011, 10:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.