当我尝试插入表格时,我得到QSqlError("","参数计数不匹配","")这里是我的查询

时间:2016-09-14 05:15:06

标签: c++ qt sqlite

   QSqlQuery insert_emi_query;
   insert_emi_query.prepare("INSERT INTO emi_info (emi-info_id, customer_id, down_payment, emi_start_date, emi_end_date, emi_amount, toatl_emi, intrest_rate, total_emi_amount) "
                 "VALUES(:emi-info_id, :customer_id, :down_payment, :emi_start_date, :emi_end_date, :emi_amount, :toatl_emi, :intrest_rate, :total_emi_amount)");
   insert_emi_query.bindValue(":emi-info_id",emi_id);
   insert_emi_query.bindValue(":customer_id",cutomer_id);
   insert_emi_query.bindValue(":down_payment",ui->txtEMIDownPayment->text().toInt());
   insert_emi_query.bindValue(":emi_start_date",ui->dateEMIStart->date());
   insert_emi_query.bindValue(":emi_end_date",ui->dateEMIEnd->date());
   insert_emi_query.bindValue(":emi_amount",ui->txtEMIPerMonth->text().toInt());
   insert_emi_query.bindValue(":toatl_emi",ui->spinEMI->text().toInt());
   insert_emi_query.bindValue(":intrest_rate",ui->txtEMIRate->text().toInt());
   insert_emi_query.bindValue(":total_emi_amount",ui->txtEMIAfterPayment->text().toInt());
   if(insert_emi_query.exec()){
       qDebug() << "EMI Info Added---------------------";
   }else{
       qDebug() << "EMi not inserted" << insert_emi_query.lastError();
   }

3 个答案:

答案 0 :(得分:1)

:emi-info_id不是有效parameter name(不带引号的标识符不允许-,并且不能引用参数名称。)

答案 1 :(得分:0)

我无法修改您的问题。您可能希望自己编辑它,使用以下代码段来提高理解力。

1)“emi-info_id”中的短划线符号是否正确?大多数数据库不允许在列名中使用破折号。

2)您可能想检查是否为表的所有必填列指定了值。

QSqlQuery insert_emi_query;

insert_emi_query.prepare("                                         "
"                                                                  "
"INSERT INTO emi_info (                                            "
"   emi-info_id, customer_id, down_payment, emi_start_date,        "
"   emi_end_date, emi_amount, toatl_emi, intrest_rate,             "
"   total_emi_amount)                                              "
"                                                                  "
"                                                                  "
"VALUES(                                                           "
"   :emi-info_id, :customer_id, :down_payment, :emi_start_date,    "
"   :emi_end_date, :emi_amount, :toatl_emi, :intrest_rate,         "
"   :total_emi_amount)                                             "
"                                                                  "
);

insert_emi_query.bindValue(":emi-info_id",emi_id);
insert_emi_query.bindValue(":customer_id",cutomer_id);
insert_emi_query.bindValue(":down_payment",ui->txtEMIDownPayment->text().toInt());
insert_emi_query.bindValue(":emi_start_date",ui->dateEMIStart->date());

insert_emi_query.bindValue(":emi_end_date",ui->dateEMIEnd->date());
insert_emi_query.bindValue(":emi_amount",ui->txtEMIPerMonth->text().toInt());
insert_emi_query.bindValue(":toatl_emi",ui->spinEMI->text().toInt());
insert_emi_query.bindValue(":intrest_rate",ui->txtEMIRate->text().toInt());
insert_emi_query.bindValue(":total_emi_amount",ui->txtEMIAfterPayment->text().toInt());

if(insert_emi_query.exec()) {
   qDebug() << "EMI Info Added---------------------";
}
else{
   qDebug() << "EMi not inserted" << insert_emi_query.lastError();
}

答案 2 :(得分:0)

您查询中的列名emi-info_id似乎是拼写错误。它应该是emi_info_id。因为-的列名无效。

如果您使用Qt5(我不知道qt4中有什么)或更高版本,那么您可以获得类似

的错误类型
insert_emi_query.lastError().type()

在上述情况下,它应为QSqlError::StatementError,因为列名称中存在拼写错误。