Hello everyone ,
I have a problem and I couldn't find why? this is my program but every time commit returns false it says commit without any open transaction.
- I use sqlite.
- I havent any open query
- All functions has local QSqlQuery so they killed after finishing
- Without transaction everything works fine
// this is my sqlDB code
// QSqlDatabase &DataManager::mainDB()
//{
// return db;
//}
//
CompanyMembersTable cmp(&sqlDB);
if(sqlDB.mainDB().transaction())
{
if(cmp.removeMembers(customer.nationalcode))
{
if(cst.updateEntity(customer.id(), &customer)) {
for(int i=0; i<ui->lstCompMem->count(); i++) {
QString nationlid
= ui
->lstCompMem
->item
(i
)->data
(Qt
::UserRole).
toString();
Customer cust;
cust.nationalid = nationlid;
if(cmp.insertMember(&customer, &cust) <= 0)
{
lerror = cmp.lastError();
break;
}
}
if(lerror.isEmpty() && sqlDB.mainDB().commit() ) {
QMessageBox::information(this, tr
("Success Edit"), tr
("Customer Updated successfully"));
emit saved();
return;
}
else if(lerror.isEmpty()) {
lerror = sqlDB.mainDB().lastError().text();
}
}
else lerror = cst.lastError();
}
else lerror = cmp.lastError();
}
else lerror = sqlDB.mainDB().lastError().text();
sqlDB.mainDB().rollback();
QMessageBox::critical(this, tr
("Edit Error"), tr
("Error Happend.\n%1").
arg(lerror
), tr
("OK"));
return;
// this is my sqlDB code
// QSqlDatabase &DataManager::mainDB()
//{
// return db;
//}
//
CompanyMembersTable cmp(&sqlDB);
QString lerror;
if(sqlDB.mainDB().transaction())
{
if(cmp.removeMembers(customer.nationalcode))
{
if(cst.updateEntity(customer.id(), &customer)) {
for(int i=0; i<ui->lstCompMem->count(); i++) {
QString nationlid = ui->lstCompMem->item(i)->data(Qt::UserRole).toString();
Customer cust;
cust.nationalid = nationlid;
if(cmp.insertMember(&customer, &cust) <= 0)
{
lerror = cmp.lastError();
break;
}
}
if(lerror.isEmpty() && sqlDB.mainDB().commit() ) {
QMessageBox::information(this, tr("Success Edit"), tr("Customer Updated successfully"));
emit saved();
return;
}
else if(lerror.isEmpty()) {
lerror = sqlDB.mainDB().lastError().text();
}
}
else lerror = cst.lastError();
}
else lerror = cmp.lastError();
}
else lerror = sqlDB.mainDB().lastError().text();
sqlDB.mainDB().rollback();
QMessageBox::critical(this, tr("Edit Error"), tr("Error Happend.\n%1").arg(lerror), tr("OK"));
return;
To copy to clipboard, switch view to plain text mode
and I should mention that the below code work fine
CustomerTable cst(&sqlDB);
CompanyMembersTable cmb(&sqlDB);
sqlDB.mainDB().transaction();
if(cst.insertEntity(&customer) > 0)
{
foreach (Customer cust, customer.members) {
if(cmb.insertMember(&customer, &cust) <= 0)
{
goto ercust;
}
}
if(sqlDB.mainDB().commit()) {
QMessageBox::information(this, tr
("Success Insert"), tr
("Customer inserted successfully"));
return;
}
}
ercust:
QMessageBox::critical(this, tr
("Error Insert"), tr
("There is an error in inserting customer\n%1").
arg(cst.
lastError()));
sqlDB.mainDB().rollback();
CustomerTable cst(&sqlDB);
CompanyMembersTable cmb(&sqlDB);
sqlDB.mainDB().transaction();
if(cst.insertEntity(&customer) > 0)
{
foreach (Customer cust, customer.members) {
if(cmb.insertMember(&customer, &cust) <= 0)
{
goto ercust;
}
}
if(sqlDB.mainDB().commit()) {
QMessageBox::information(this, tr("Success Insert"), tr("Customer inserted successfully"));
return;
}
}
ercust:
QMessageBox::critical(this, tr("Error Insert"), tr("There is an error in inserting customer\n%1").arg(cst.lastError()));
sqlDB.mainDB().rollback();
To copy to clipboard, switch view to plain text mode
I want to know in witch situations a transaction would be canceled automatically.
thanks in advance
Bookmarks