2

Hello I am trying to insert multiple rows (two in this case) into a SQLITE database using qt 5.4. I have been following the documentation but the program has been crashing and I can not explain why.

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();
3
  • How have you initialized your database? Commented Mar 3, 2015 at 17:26
  • yes, i have connected to the database and have created the table with the corresponding fields Commented Mar 3, 2015 at 17:31
  • I see no closing ) in the query string... Commented Mar 3, 2015 at 18:03

1 Answer 1

3

Start by trying to successfully insert one row. You'll need to specify which columns in the table that the values correspond to.

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

Also, addBindValue( DateTime ) should be addBindValue( Date_Time ).

And,

query_Invoice.prepare("INSERT INTO Invoice VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); <---- Closing paren after last '?'
Sign up to request clarification or add additional context in comments.

4 Comments

I am abel to insert 1 single row into the database
Did you try adding the column names?
Added another suggestion. DateTime to Date_Time in addBindValue()
Changed DateTime to Date_Time and it is working, thank you very much for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.