PDA

View Full Version : Sqlite commit says no transaction is open



danics
15th June 2017, 19:13
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);

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;


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();


I want to know in witch situations a transaction would be canceled automatically.
thanks in advance

jefftee
17th June 2017, 06:11
You'll need to post more of your code, I have no idea what Customer and CompanyMembersTable is doing for example