PDA

View Full Version : QSqlDatabase and execution query time: increases each time!



Wing_Zero
18th August 2010, 03:28
Hi at all,
I'm sorry to be annoying but I'm new to qt and sometimes there is something that i really can't understand :(

Specifically, I've a QSQLITE DB and when i try to do an insert or update query (no problem with select query) just like this:


QString query = "UPDATE MovieDB SET ...etc...";
QSqlQuery queryResult= QSqlQuery (db);
queryResult.exec (query);
std::cerr << qPrintable(queryResult.lastError().text());
emit doneUp();

each time i invoke this function the time take to execute the query increase by one step.
For example if the first time I take 0.5 seconds. the second time 1.0 seconds. then 1.5 seconds, and so on...
I'm really in panic because I think there is nothing I can do to fix this...
The lag problem, unluckily, it is only a query execution fault: in fact commenting that line [ queryResult.exec (query); ] the lag disapper.
I don't know how to solve that.

I appreciate every help, and thanks is advance.

As usually sorry for my bad english :(

Gianluigi

tbscope
18th August 2010, 06:49
Can you try if a transaction will increase performance?



QString query = "UPDATE MovieDB SET ...etc...";

db.transaction();

QSqlQuery queryResult= QSqlQuery (db);
queryResult.exec (query);

db.commit();

std::cerr << qPrintable(queryResult.lastError().text());
emit doneUp();

Wing_Zero
18th August 2010, 11:15
First of all: thanks for your reply! :)

I've just tried: it do the same job -.-"

I Think it is not a problem of performance itself...it is something like a bug in qsqlquery class...or a bug in the sqlite db...
It seems like if every time it execute the query more than once...the first time it takes 0,5s, the second time i do the same query it take 1,0s, the third time 1,5s...it is linear...and it is very strange.

Lesiok
18th August 2010, 11:50
Can You create small compilable example with this behaviour ?

Lykurg
18th August 2010, 11:51
Sine the execution time constantly increases by the first "time duration", it sounds like you have somewhere an error in your code. Is your "QString query" always the same (with different values of course) or maybe you add new instructions to your query that the old ones also gets executed? How do you measure the time?

Can you provide a minimal example reproducing your problem?

Wing_Zero
18th August 2010, 12:44
Solved . Sorry for wasting your time :(
Lykurg, you was right.
The error was that the QObject::connect that calls the updatemethod (as a slot) was inserted in a for-cicle.
I thought that even if the qobject::connect was setted 2,3 o more times the slot was called just one time. evidently I was wrong.
The problem was something like this:

QObject::connect(client SIGNAL(doUpdate(QString)), Database, SLOT (updateDB(QString)));
QObject::connect(client SIGNAL(doUpdate(QString)), Database, SLOT (updateDB(QString)));
.
.
.
QObject::connect(client SIGNAL(doUpdate(QString)), Database, SLOT (updateDB(QString)));// n times the connect was setted

And then the update query will be executed N times for each single time that signal is emitted. I though instead that the query was executed just one time.
I know that's stupid mistake i've done, but documentation lack in this point.

Thanks at all and sorry again

Gianluigi

Lykurg
18th August 2010, 13:52
You can use Qt::UniqueConnection if you are unsure if the connection is already established and you only want the connection only once.