c++ - how to make message show once using QMessage -
i trying make message show once if query in loop executes successfully. when this;
qsqlquery query1; (int = 0; < itemedits.size(); i++) { query1.prepare("insert class1_bills (bill_item, amount) values (:bill_item, :amount)"); query1.bindvalue(":bill_item", ""+itemedits[i]->text()+""); query1.bindvalue(":amount", ""+amountedits[i]->text()+""); } if (query1.exec()) { close(); qmessagebox::warning(0, "success", "bill class 1 prepared"); } else { close(); qmessagebox::warning(0, "failed", "bill class 1 not prepared"); }
the query inserts 1 record in table. i.e. if supposed insert 3 record through loop table, once. , if this;
qsqlquery query1; (int = 0; < itemedits.size(); i++) { query1.prepare("insert class1_bills (bill_item, amount) values (:bill_item, :amount)"); query1.bindvalue(":bill_item", ""+itemedits[i]->text()+""); query1.bindvalue(":amount", ""+amountedits[i]->text()+""); if (query1.exec()) { close(); qmessagebox::warning(0, "success", "bill class 1 prepared"); } else { close(); qmessagebox::warning(0, "failed", "bill class 1 not prepared"); } }
the message shows number of times equivalent loop. want message show once regardless number of times loop executes. how do it?
in 1st code, you're executing query outside loop when supposed in loop inserts number of records being looped. makes query execute once.
in 2nd code, you're doing right thing executing query in loop inserts number of records being looped. you're putting message should show 'once' in loop well, makes message show every time record inserted table loop. solve this, assign execution boolean
variable inside loop has been declared (maybe, initialized because compiler might send warning saying it maybe used uninitialized
) outside loop , use in if
statement so;
qsqlquery query1; //declared , initialized here bool ok = 0; (int = 0; < itemedits.size(); i++) { query1.prepare("insert class1_bills (bill_item, amount) values (:bill_item, :amount)"); query1.bindvalue(":bill_item", ""+itemedits[i]->text()+""); query1.bindvalue(":amount", ""+amountedits[i]->text()+""); //assigned here ok = query1.exec(); } //used here if (ok) { close(); qmessagebox::warning(0, "success", "bill class 1 prepared"); } else { close(); qmessagebox::warning(0, "failed", "bill class 1 not prepared"); }
this should solve problem.
Comments
Post a Comment