Qt World Summit: Submit your Presentation

QSqlQuery issue

  • Hi All,

    I am developing a qt application using a SQLite database. In fact, I collect data periodically and I store them in the database.

    The code compiles fine but after a few minutes, the application craches. I tried to debug the application using the gdb tool. Here is what the debugger appears:

    • __memmove_ssse3_back () at ../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S:1547
      1547 ../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S: Aucun fichier ou dossier de ce type.
      (gdb) where
      #0 __memmove_ssse3_back () at ../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S:1547
      #1 0x0000000000dbd399 in QListData::remove(int) ()
      #2 0x00000000004b1f11 in QList<QSQLiteResult*>::removeOne(QSQLiteResult* const&) ()
      #3 0x00000000004b0116 in QSQLiteResult::~QSQLiteResult() ()
      #4 0x00000000004b0189 in QSQLiteResult::~QSQLiteResult() ()
      #5 0x000000000049f4ae in QSqlQueryPrivate::~QSqlQueryPrivate() ()
      #6 0x000000000049f7df in QSqlQuery::~QSqlQuery() ()
      #7 0x0000000000545a20 in QSqlQueryModelPrivate::~QSqlQueryModelPrivate() ()
      #8 0x0000000000541ff8 in QSqlTableModelPrivate::~QSqlTableModelPrivate() ()
      #9 0x0000000000f6e7a3 in QObject::~QObject() ()
      #10 0x0000000000439516 in WritingData::updateNodeStatus(QString, int) ()
      #11 0x0000000000414a53 in CollectScheduler::updateNodeStatus(QString, int) ()
      #12 0x000000000041635b in CollectScheduler::setWorkerPing(QString, bool) ()
      #13 0x0000000000453dd4 in CollectScheduler::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) ()
      #14 0x0000000000f67be6 in QObject::event(QEvent*) ()
      #15 0x000000000056442c in QApplicationPrivate::notify_helper(QObject*, QEvent*) ()
      #16 0x00000000005692e6 in QApplication::notify(QObject*, QEvent*) ()
      #17 0x0000000000f451e0 in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) ()
      #18 0x0000000000f8525c in QEventDispatcherUNIX::processEvents(QFlagsQEventLoop::ProcessEventsFlag) ()
      #19 0x0000000000f403d1 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag >) ()
      #20 0x0000000000d54568 in QThread::run() ()
      #21 0x0000000000d598ef in QThreadPrivate::start(void*) ()
      #22 0x00007ffff6174182 in start_thread (arg=0x7ffff3140700) at pthread_create.c:312
      #23 0x00007ffff568147d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111**

    How can i fix this bug?
    Any help would be appreciated.

  • Hi and welcome to devnet,

    very hard to help without the code.
    BTW, the debug info says that the error is inside WritingData::updateNodeStatus

  • @mcosta

    Thanks for your reply, here the funtion WritingData::updateNodeStatus

    bool WritingData::updateNodeStatus(QString node_Id , int node_status)
    bool submit=false;
    QSqlTableModel model(NULL, writingDb);
    model.setFilter(QString("node_id = '%1'").arg(node_Id));
    if (model.rowCount() == 1) {
    QSqlRecord record = model.record(0);
    record.setValue("node_id", node_Id);
    record.setValue("node_status_id", QString::number(node_status));
    model.setRecord(0, record);
    return submit;

    This function is called by another thread for updating the last pool column of a specified node (filtered by its id). Moreover, this function uses a writing connexion (called here "writingDb").

  • Hi,

    the error happens in the QSqlModel destructor. (you should run in debug mode to try to understand why it crashes)

    QUESTION: are you sure your code does what you want? each time you overwrite the first record without storing all data model.setRecord(0, record)

  • @mcosta

    I have tried to use another method to update the database. Here the implementation of the new function: (this function updates the status of one node (network node), that's why i overwrite each time the first record)

    bool WritingData::updateNodeStatus(QString node_Id , int node_status)
        bool submit=false;
            QSqlQuery query(writingDb);
            submit=query.exec(QString("UPDATE nodes set node_status_id='%1' where   node_id='%2'").arg(QString::number(node_status)).arg(node_Id));
        return submit;

    There still the same problem. I think the problem is caused by the function

    Note : I have this problem only on Linux Os (ubuntu 14.04). It doesn't crash on windows OS.

    Have you any idea?

    Edited: Please use ``` (3 back-ticks) for code blocks - p3c0

  • @fahmouch said:


    I think is a QSqlDatabase instance. Is it used across threads??
    According to this you should use it only in the thread that created it

  • @mcosta


    I read in the offical documentation this phrase : The driver is locked for updates while a select is executed. This may cause problems when using QSqlTableModel because Qt's item views fetch data as needed (with QSqlQuery::fetchMore() in the case of QSqlTableModel).

    In fact, i tested my application by disabling the reading component (thread) and i haven't any problem. That's why, i think that i have this problem.

    There is any way to solve this problem?

  • Hi,

    the solution is to use the connection only inside the GUI thread. Or, if you need to work with multiple threads, you have to use more connections.

  • @mcosta

    I'm already using two connections (readingDB and writingDB) and each connection has a specific name.

  • mmmm,

    the problem is that SQLite (the DB not the Qt driver) cannot handle multiple connections in the right way (is a file access problem).
    DO you need to open the database in ReadWrite mode in both threads (do you need to modify through the view)??
    If the view is read-only you can try to specify QSQLITE_OPEN_READONLY for the UI thread connection

  • @mcosta

    Thanks for your reply,

    I have already tried this solution, but unfortantly, it doesn't resolve the problem (after a few minutes, the application crashes on Linux).

Log in to reply