How to start an immediate transaction of Sqlite in Qt?

254 Views Asked by At

Got troubles with using QSqlDatabase::transaction(). I want to start a transaction of Sqlite in Qt and I know this function : bool QSqlDatabase::transaction():

if(db.transaction())
{
    QSqlQuery query(db);
    // do stuff

   if(!db.commit())
   {
        qDebug() << "Failed to commit";
        db.rollback();
   }
}

My problem is that I want to start an immediate transaction as follows:

> begin immediate;
> ...(operation)
> commit;

This function bool QSqlDatabase::transaction() will start a default transaction. There will be several users using this program to access sqlite database so I need to control their access orders. If one user is writing, others can't read or write. It seems that I need to start an immediate transaction.

So how to start an immediate transaction by QSqlDatabase::transaction() ? Is there way to specify transaction type in QSqlDatabase::transaction() ?

I use this to start an immediate transaction:

QsqlQuery query(myDatabase);
query.exec("begin immediate;");
query.exec(...) // read or write database.
query.exec("commit;");

It didn't work. There is still wrong data caused by multiple user access in database.

0

There are 0 best solutions below