PDA

View Full Version : QSqlQuery exec blocking function



reinki0013
10th August 2012, 09:08
hi,

my problem is that the
QSqlQuery::exec() function is blocking my application when the network-connection is down.

i tried to set the flag
MYSQL_OPT_RECONNECT=1 with
QSqlQuery::setConnectOptions() but this doesn`t help.

is there any workaround about this or what can i do to solve this problem?

kind regards,
reinki

reinki0013
13th August 2012, 05:41
Can anybody help me with my problem?

Lykurg
13th August 2012, 08:47
You can first check if the network connection is down. Or put all database interaction in a thread.

reinki0013
13th August 2012, 14:08
hey,

in my case it could be, that the network-connection is up (before the exec()) and during the exec() it could be down - so it blocks my application.
Before the exec() i watched with a thread if the network-connection is down - but during the exec() i don`t have any chance.

so what can i do?

Added after 24 minutes:

I`ve tried to build a QThread with my .exec():

Implementation in my QThread-Classe:


void cExecThread::run()
{
msleep(500);
QSqlDatabase db = m_Database;
db.exec(m_Query);
}

In my class where I need this exec() I do it like this:


void cDatabase::RemoveBusNr(QString TableName, QString BusNr)
{
if(db.isValid() && m_Connected)
{
QSqlQuery sql_query(db);

QString query = "DELETE FROM `";
query += TableName + "` WHERE `BusNr` = '";
query += BusNr += "';";

Exec(query);
//sql_query.exec(query);
CheckSqlError();
//qDebug() << query;
}
}

And here is the implementation from my Exec()-Function:


void cDatabase::Exec(QString query)
{
execThread.m_Database = db;
execThread.m_Query = query;
execThread.start();

int iCount = 0;

while(execThread.isRunning())
{
if(iCount < 5000)
iCount++;
else
{
execThread.terminate();

if(execThread.isFinished())
qDebug() << "Thread finished";

}
}
}


But this doesn`t work very well - it works wrong and delayed my programm.

yeye_olive
13th August 2012, 15:47
The whole point of using threads is to prevent your GUI thread to be frozen during the blocking exec() call, but your code busy waits for the worker thread to finish and terminates it brutally after a while. I suggest you use a higher-level alternative like QtConcurrent::run() instead, it will be a lot easier. Read the docs and examples on QtConcurrent first.

Lykurg
13th August 2012, 18:00
And also read about SQL-Injections!!! Use QSqlQuery::prepare()... Further do not share a database connection between threads because they can crash.