Results 1 to 6 of 6

Thread: QSqlQuery exec blocking function

  1. #1
    Join Date
    May 2009
    Location
    Austria
    Posts
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default QSqlQuery exec blocking function

    hi,

    my problem is that the
    Qt Code:
    1. QSqlQuery::exec()
    To copy to clipboard, switch view to plain text mode 
    function is blocking my application when the network-connection is down.

    i tried to set the flag
    Qt Code:
    1. MYSQL_OPT_RECONNECT=1
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. QSqlQuery::setConnectOptions()
    To copy to clipboard, switch view to plain text mode 
    but this doesn`t help.

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

    kind regards,
    reinki

  2. #2
    Join Date
    May 2009
    Location
    Austria
    Posts
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery exec blocking function

    Can anybody help me with my problem?

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlQuery exec blocking function

    You can first check if the network connection is down. Or put all database interaction in a thread.

  4. #4
    Join Date
    May 2009
    Location
    Austria
    Posts
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery exec blocking function

    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:
    Qt Code:
    1. void cExecThread::run()
    2. {
    3. msleep(500);
    4. QSqlDatabase db = m_Database;
    5. db.exec(m_Query);
    6. }
    To copy to clipboard, switch view to plain text mode 
    In my class where I need this exec() I do it like this:

    Qt Code:
    1. void cDatabase::RemoveBusNr(QString TableName, QString BusNr)
    2. {
    3. if(db.isValid() && m_Connected)
    4. {
    5. QSqlQuery sql_query(db);
    6.  
    7. QString query = "DELETE FROM `";
    8. query += TableName + "` WHERE `BusNr` = '";
    9. query += BusNr += "';";
    10.  
    11. Exec(query);
    12. //sql_query.exec(query);
    13. CheckSqlError();
    14. //qDebug() << query;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void cDatabase::Exec(QString query)
    2. {
    3. execThread.m_Database = db;
    4. execThread.m_Query = query;
    5. execThread.start();
    6.  
    7. int iCount = 0;
    8.  
    9. while(execThread.isRunning())
    10. {
    11. if(iCount < 5000)
    12. iCount++;
    13. else
    14. {
    15. execThread.terminate();
    16.  
    17. if(execThread.isFinished())
    18. qDebug() << "Thread finished";
    19.  
    20. }
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 

    But this doesn`t work very well - it works wrong and delayed my programm.
    Last edited by reinki0013; 13th August 2012 at 14:08.

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery exec blocking function

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlQuery exec blocking function

    And also read about SQL-Injections!!! Use QSqlQuery::prepare()... Further do not share a database connection between threads because they can crash.

Similar Threads

  1. Abort QSqlQuery::exec()
    By elmo in forum Qt Programming
    Replies: 5
    Last Post: 13th October 2013, 20:50
  2. Replies: 1
    Last Post: 18th July 2011, 12:12
  3. QSqlQuery.exec() weird error
    By MarkoSan in forum Qt Programming
    Replies: 3
    Last Post: 25th May 2010, 13:02
  4. QSqlQuery and no response while exec()
    By jacek_ in forum Qt Programming
    Replies: 5
    Last Post: 5th November 2009, 08:47
  5. exec() not blocking, derived QDialog, Qt 4.4.3
    By wdezell in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2009, 18:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.