PDA

View Full Version : Problems with QSqlQuery update



whoops.slo
27th August 2006, 09:41
Hi!

In my code I have several QSqlQuery statements, each has different name and are used for selecting data and updating that data. When I select data from a table using the select query I caould not update that same table using update query until I cleared that query using QsqlQuery::clear(). Why is that necessary?

Example code (not necessary working, just to see what I meant):

QSqlQuery q_select1("SELECT * FROM table1 WHERE number LIKE '1'");
int i_word = q_select1.record().value("word");
q_select1.next();
QString s_word = q_select1.value(i_word);

q_select1.clear();

QSqlQuery q_update1("UPDATE table1 SET word = 'new word' WHERE number = '1'");
Why the q_update1 did not work until i put q_select1 there?

Thanks!
Luka

jacek
27th August 2006, 13:53
Maybe SELECT statement locks the table? What database do you use?

whoops.slo
27th August 2006, 23:36
i use sqlite 3. i also think that select lock the table, i just wanted to be sure that i am not doing something wrong and that this is the correct behaviour of the qt and sqlite. is it possible to do another select statement on the previously locked table but update would not work?

jacek
28th August 2006, 00:13
i use sqlite 3. i also think that select lock the table, i just wanted to be sure that i am not doing something wrong and that this is the correct behaviour of the qt and sqlite.
I don't use SQLite, but probably you can use "PRAGMA read_uncommitted = 1" as a workaround, but beware.


is it possible to do another select statement on the previously locked table but update would not work?
Yes, SELECT acquires a SHARED lock which allows only reading, but UPDATE tries to acquire an EXCLUSIVE lock which can't coexist with any other locks, thus it fails.

whoops.slo
28th August 2006, 08:17
thanks, that is what i wanted to know. now everything makes sence and my speculation was correct.