PDA

View Full Version : SQLITE Transactions



mcosta
6th April 2009, 17:57
Hi all,

I've noticed that QSqlDatabase::transaction() and QSqlDriver::beginTransaction() don't work when using QSQLITE driver.

Using QSqlQuery::exec("BEGIN TRANSACTION") work correctly.

It's a bug?

PS. both QSqlDatabase::transaction() and QSqlDriver::beginTransaction() return true for correct execution

Example code


QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

db.transaction(); // returns true
for (int i = 0; i < 1000; ++i)
{
// INSERT data
}
db.commit(); // returns true

doesn't work



QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

QSqlQuery q(db);
q.exec("BEGIN TRANSACTION");
for (int i = 0; i < 1000; ++i)
{
// INSERT data
}
q.exec("COMMIT");

works correctly.

talk2amulya
6th April 2009, 19:30
yes, it doesnt work cuz for SQLite driver, in the transaction() function, Qt only executes "BEGIN" instead of "BEGIN TRANSACTION"..now why they did that is unclear..i think even PostgreSQL also has that limitation..dont clearly remember..ciao

janus
7th April 2009, 09:00
HI,

AFIAK there is no difference in sqlite using "BEGIN" or "BEGIN TRANSACTION"

talk2amulya
7th April 2009, 10:16
hmm..i checked it on my end again and guess what, its working..i went through the code for sqlite in Qt and confirmed they R executing just BEGIN..so i guess, there must be something missing in your code..

talk2amulya
7th April 2009, 20:45
q.exec("BEGIN TRANSACTION");
for (int i = 0; i < 1000; ++i)
{
// INSERT data
}
q.exec("COMMIT");

why dont u try executing just "BEGIN" at the start of the loop, just like sqlite does and see what happens