QSqlQuery插入数据操作实例,包含批处理代码

void KaiDanWnd::on_action_4_triggered() { if(ui->lineEdit->text()==tr("")) { QMessageBox::about(this,tr("数据填写不完整"),tr("客户姓名未填写")); return; } if(ui->lineEdit_3->text()==tr("")) { QMessageBox::about(this,tr("数据填写不完整"),tr("客户电话未填写")); return; } if(ui->lineEdit_2->text()==tr("")) { QMessageBox::about(this,tr("数据填写不完整"),tr("客户地址未填写")); return; } if(ui->lineEdit_6->text()==tr("")) { QMessageBox::about(this,tr("数据填写不完整"),tr("已付金额未填写")); return; } if(ui->tableWidget->rowCount()==0) { QMessageBox::about(this,tr("数据填写不完整"),tr("当前订单中没有任何产品数据")); return; } SqliteDB db; db.connDb(); //插入客户表开始 QString sqlstring=tr("insert into 客户表 (开单日期,客户姓名,联系方式,客户地址,是否已结帐,总金额,已付金额,未付金额,销售人员,备注) values(?,?,?,?,?,?,?,?,?,?)"); QSqlQuery query; query.prepare(sqlstring); query.addBindValue(ui->dateEdit->date()); query.addBindValue(ui->lineEdit->text().trimmed()); query.addBindValue(ui->lineEdit_3->text().trimmed()); query.addBindValue(ui->lineEdit_2->text().trimmed()); query.addBindValue(ui->comboBox->currentText()); query.addBindValue(ui->lineEdit_8->text().toDouble()); query.addBindValue(ui->lineEdit_6->text().toDouble()); query.addBindValue(ui->lineEdit_7->text().toDouble()); QString xiaoshourenyuan; if(ui->comboBox_2->currentText().trimmed()!="") { xiaoshourenyuan=xiaoshourenyuan+ui->comboBox_2->currentText().trimmed()+tr(" * "); } if(ui->comboBox_3->currentText().trimmed()!="") { xiaoshourenyuan=xiaoshourenyuan+ui->comboBox_3->currentText().trimmed()+tr(" * "); } if(ui->comboBox_4->currentText().trimmed()!="") { xiaoshourenyuan=xiaoshourenyuan+ui->comboBox_4->currentText().trimmed()+tr(" * "); } if(ui->comboBox_5->currentText().trimmed()!="") { xiaoshourenyuan=xiaoshourenyuan+ui->comboBox_5->currentText().trimmed()+tr(" * "); } query.addBindValue(xiaoshourenyuan.trimmed()); query.addBindValue(ui->lineEdit_21->text().trimmed()); int flag=0; if(query.exec()) { flag++; //QMessageBox::about(this,tr("客户表"),tr("提交订单成功!")); } //插入客户表结束 //取得插入的客户编号 query.clear(); query=db.search(tr("select 客户编号 from 客户表")); query.last(); QString bianhao=query.record().value(0).toString(); //QMessageBox::about(this,"",bianhao); //插入客户付款记录表开始 query.clear(); query.prepare("insert into 客户付款记录表 (客户编号, 付款日期, 付款金额, 备注) values(?,?,?,?)"); query.addBindValue(bianhao); query.addBindValue(ui->dateEdit->date()); query.addBindValue(ui->lineEdit_6->text().toDouble()); query.addBindValue(tr("定金")); if(query.exec()) { flag++; //QMessageBox::about(this,tr("客户付款记录表"),tr("提交订单成功!")); } //插入客户付款记录表结束 //插入卖出账目表开始 query.clear(); QString sql=tr("insert into 卖出账目表 " "(客户编号, 操作时间, 补退货, 产品名称, 产品型号, 产品规格, 备注, 单位, 卖出单价, " "总数量, 已发货数量, 店里货数量, 另外采购数量, 金额, 是否发货完成)" "values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); //QMessageBox::about(this,"",sql); query.prepare(sql); QVariantList list1,list2,list3,list4,list5,list6,list7,list8,list9,list10,list11,list12,list13,list14,list15,list16; for(int i=0;i<ui->tableWidget->rowCount();i++) { list1<<bianhao; list2<<ui->dateEdit->date(); list3<<tr("开单"); list4<<ui->tableWidget->item(i,0)->text().trimmed(); list5<<ui->tableWidget->item(i,1)->text().trimmed(); list6<<ui->tableWidget->item(i,2)->text().trimmed(); list7<<ui->tableWidget->item(i,3)->text().trimmed();//备注 list8<<ui->tableWidget->item(i,4)->text().trimmed(); list9<<ui->tableWidget->item(i,5)->text().toDouble(); list10<<ui->tableWidget->item(i,6)->text().toDouble(); list11<<0; list12<<0; list13<<0; list14<<ui->tableWidget->item(i,7)->text().toDouble(); list15<<tr("否") ; } query.addBindValue(list1); query.addBindValue(list2); query.addBindValue(list3); query.addBindValue(list4); query.addBindValue(list5); query.addBindValue(list6); query.addBindValue(list7); query.addBindValue(list8); query.addBindValue(list9); query.addBindValue(list10); query.addBindValue(list11); query.addBindValue(list12); query.addBindValue(list13); query.addBindValue(list14); query.addBindValue(list15); //QMessageBox::about(this,QString::number(list1.count()),sql); if(!query.execBatch())//执行批处理 { QMessageBox::about(this,"",query.lastError().text()); } else { flag++; } //插入买入账目完成 if(flag==3) { QMessageBox::about(this,tr("操作成功"),tr("提交订单成功!")); } this->parentWidget()->close(); }

你可能感兴趣的:(sql,list,query,insert,action,产品)