lundi 4 mai 2020

What are the solution of QSqlError("", "Parameter count mismatch", "") error?

I get this message "QSqlError("", "Parameter count mismatch", "")" every time. I cant find any problem in my code. I find google and stackoverflow, can not find any proper solution. Please help me.I want to understand why give this error.

  void DB_MAN::conDB()
{
    //create database
    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("phonebook.db");

    if(!database.isOpen()){
        database.open ();
        qDebug() << "Database connected.";
    } else {
        qDebug() << database.lastError().databaseText();

    }

    //set query db
    query = new QSqlQuery(database);
}

Another class:

QString  name = ui->txt_Name->text();
QString cont = ui->txt_contact->text();
int id = ui->txt_id->text().toInt();

const QString save_stmnt = "insert into contact(ID) values(:id)";

db->getQuery()->prepare(save_stmnt);

db->getQuery()->bindValue(":id",id);
//db->getQuery()->bindValue(":name",name);
//db->getQuery()->bindValue(":cont",cont);

if(db->getQuery()->exec()){
    qDebug() << "saved";
    db->getDatabase().close();
} else {
    qDebug() << db->getQuery()->lastError();
    qDebug() << db->getDatabase().tables();
    qDebug() << db->getQuery()->lastQuery();

}

Aucun commentaire:

Enregistrer un commentaire