使用qt在SQLITE数据库中插入多行

时间:2015-03-03 17:15:36

标签: c++ database qt sqlite

您好我试图使用qt 5.4将多行(在本例中为两行)插入SQLITE数据库。我一直在关注文档,但程序崩溃了,我无法解释原因。

query_Invoice.prepare("INSERT INTO Invoice VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
QVariantList Invoice_Number, Date_Time, Total_Purchased, Qty, Description, Product_CODE, Unit_Price, Total_Price, Goods_Total, VAT_Invoice, Company_ID;
Invoice_Number << 1 << 2;
Date_Time << 1776-07-04 << 1776-07-05;
Total_Purchased << 2 << 3;
Qty << 50 << 60;
Description << "Paint" << "Drill" << QVariant(QVariant::String);
Product_CODE << "EEFF2" << "EEF23" << QVariant(QVariant::String);
Unit_Price << 12 << 13;
Total_Price << 53 << 66;
Goods_Total << 70 << 80;
VAT_Invoice << 55 << 66;
Company_ID << 1 << 2;
query_Invoice.addBindValue(Invoice_Number);
query_Invoice.addBindValue(DateTime);
query_Invoice.addBindValue(Total_Purchased);
query_Invoice.addBindValue(Qty);
query_Invoice.addBindValue(Description);
query_Invoice.addBindValue(Product_CODE);
query_Invoice.addBindValue(Unit_Price);
query_Invoice.addBindValue(Total_Price);
query_Invoice.addBindValue(Goods_Total);
query_Invoice.addBindValue(VAT_Invoice);
query_Invoice.addBindValue(Company_ID);
query_Invoice.execBatch();
qDebug() << query_Invoice.executedQuery();
qDebug() << query_Invoice.lastError();

1 个答案:

答案 0 :(得分:3)

首先尝试成功插入一行。您需要指定表中哪些列与值对应。

query_Invoice.prepare( "INSERT INTO Invoice ( column_name1, column_name2, column_name3 ) VALUES ( ?,?,? )" );

此外,addBindValue( DateTime )应为addBindValue( Date_Time )

query_Invoice.prepare("INSERT INTO Invoice VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); <---- Closing paren after last '?'