Important: Please read the Qt Code of Conduct -


  • Hallo

    I got some failures with QMYSQL (QSQLITE will work)

    My connection will be

    if (dbType == "QSQLITE") {
                currentConnection = QSqlDatabase::addDatabase("QSQLITE", "CN");
                currentConnection.setDatabaseName(QString(dataDir + "/%1-%2.db").arg(date.year()).arg(basename));
            } else if (dbType == "QMYSQL") {
                QString userName = settings.value("DB_userName", "QRK").toString();
                QString password = settings.value("DB_password", "").toString();
                QString hostName = settings.value("DB_hostName", "localhost").toString();
                currentConnection = QSqlDatabase::addDatabase("QMYSQL", "CN");
            bool ok =;

    When i was connected and to a simple query i got, but not allways a SIGSEGV. When i remove the connection options i got "Lost connection to MySQL server during query"
    no matter if MySQL Server is localhost or any Server on the Internet.

    Yes, i search but found no solution. What can i do false?

    OS: Linux Ubuntu (16.04 and 17.10) Server MariaDB from the Ubuntu Package

    A Simply Query which returns one record and the Database holds max. 10 Entries.

        QSqlDatabase dbc = QSqlDatabase::database("CN");
            QSqlQuery query(dbc);
            query.prepare("SELECT strValue FROM globals WHERE name='shopName'");
            name = query.value(0).toString();

    thx Chris

  • Lifetime Qt Champion


    What version of Qt are you using ?

  • Hi
    Oh, sorry
    QT5.9.1 from Ubuntu 17.10

    But also with QT5.6 on Ubuntu 16.04
    lg chris

  • Lifetime Qt Champion

    Can you show the stack trace ?

    By the way, 5.9.4 just got released, it's worth checking if it behaves better.

  • Hi
    Hope this helps ...

    signal 11 (Speicherzugriffsfehler), address is 0x1b0 from 0x7fffcff1e5ea
    [bt]: (1) /usr/lib/x86_64-linux-gnu/ [0x7fffcff1e5ea]
    [bt]: (2) /usr/lib/x86_64-linux-gnu/ [0x7fffcff1e5ea]
    [bt]: (3) /usr/lib/x86_64-linux-gnu/ [0x7fffcff1f65c]
    [bt]: (4) /usr/lib/x86_64-linux-gnu/ [0x7fffcff13bdc]
    [bt]: (5) /usr/lib/x86_64-linux-gnu/ [0x7fffcff0e516]
    [bt]: (6) /usr/lib/x86_64-linux-gnu/ [0x7fffcff0e8a6]
    [bt]: (7) /usr/lib/x86_64-linux-gnu/qt5/plugins/sqldrivers/ [0x7fffd050370c]
    [bt]: (8) /usr/lib/x86_64-linux-gnu/ [0x7ffff64260d0]
    [bt]: (9) /usr/lib/x86_64-linux-gnu/ [0x7ffff6417008]
    [bt]: (10) /home/chris/Dokumente/projects/qt/build-QRK-Desktop-Debug/qrkcore/ [0x7ffff791f2aa]
    [bt]: (11) /home/chris/Dokumente/projects/qt/build-QRK-Desktop-Debug/qrkcore/ [0x7ffff7992e6a]
    [bt]: (12) /home/chris/Dokumente/projects/qt/build-QRK-Desktop-Debug/qrkcore/ [0x7ffff7993e80]
    [bt]: (13) /home/chris/Dokumente/projects/qt/build-QRK-Desktop-Debug/qrkcore/ [0x7ffff7993e03]
    [bt]: (14) /home/chris/Dokumente/projects/qt/build-QRK-Desktop-Debug/qrkcore/ [0x7ffff7993d6d]
    [bt]: (15) /usr/lib/x86_64-linux-gnu/ [0x7ffff5f729ff]
    [bt]: (16) /usr/lib/x86_64-linux-gnu/ [0x7ffff5f7f3b7]
    [bt]: (17) /usr/lib/x86_64-linux-gnu/ [0x7ffff5f7f718]
    [bt]: (18) /usr/lib/x86_64-linux-gnu/ [0x7ffff5f7340b]
    [bt]: (19) /usr/lib/x86_64-linux-gnu/ [0x7ffff6f0746c]
    [bt]: (20) /usr/lib/x86_64-linux-gnu/ [0x7ffff6f0ed34]
    [bt]: (21) /usr/lib/x86_64-linux-gnu/ [0x7ffff5f43de8]
    [bt]: (22) /usr/lib/x86_64-linux-gnu/ [0x7ffff5f9b91e]
    [bt]: (23) /usr/lib/x86_64-linux-gnu/ [0x7ffff5f9c0e1]
    [bt]: (24) /lib/x86_64-linux-gnu/ [0x7ffff2a69fb7]
    [bt]: (25) /lib/x86_64-linux-gnu/ [0x7ffff2a6a1f0]
    [bt]: (26) /lib/x86_64-linux-gnu/ [0x7ffff2a6a27c]
    [bt]: (27) /usr/lib/x86_64-linux-gnu/ [0x7ffff5f9c47f]
    [bt]: (28) /usr/lib/x86_64-linux-gnu/ [0x7ffff5f41e3a]
    [bt]: (29) /usr/lib/x86_64-linux-gnu/ [0x7ffff5d613ca]
    [bt]: (30) /usr/lib/x86_64-linux-gnu/ [0x7ffff5d6629d]
    [bt]: (31) /lib/x86_64-linux-gnu/ [0x7ffff5aa27fc]
    [bt]: (32) /lib/x86_64-linux-gnu/ [0x7ffff5232b5f]

    lg Chris

  • @ckvsoft
    At what precise point from your code does this SEGV occur? It looks like it's just in your query.prepare() statement, without even getting as far as the execute, is that what you find stepping through?

    Also, why do you think you're having to specify all those timeout/reconnect options, else you say it loses connection. It shouldn't be losing connection anyway. Unless that's an artefact of an underlying problem which sometimes SEGVs and sometimes doesn't...

  • @JonB
    This was a test while without these Options i got a "Lost connection to MySQL server during query" without SEGV but the Query was not made. The Server is running normaly without troubles Its a TCP connection while not on the same machine. I test an other Server with do the same. Tomorow i will test a localhost machine.

    Its not allways the same query and not allways the prepare. Sometimes it is by "QSqlQuery query(dbc)" and somtime by the exec.

  • Lifetime Qt Champion

    What is that qrkcore library you are using ?

  • @SGaist
    This is a Part of my Application . Why?

  • Lifetime Qt Champion

    Well... To know whether it's an external library you are using or something you are implementing.

    Do you have the same crash if you implement just a minimal application that connects to the database and does the queries ?

  • @SGaist
    I will try this at weekend. Also i will test if MySQL and MariaDB has the same. The queries where made from the library. Thx for the time

  • Hi
    I connect to a localhost mysql server and it seems to work. When i has running in the background some sql queries and i do at the same time from other window with the same connection any query it works.

    When i connect to a server via TCP it will crash.
    Edit: Its also with localhost, but the time is a bit longer ;(

  • Lifetime Qt Champion

    Can you trigger also that when using an application that's not using the QSql** classes ?

  • I think i have found a solution ;-) Some Test will be good. I will enhance my tests.
    The Idea i found by reading

    Just my Application worked since 2015 with QSQLITE without troubles. But some users will also use MySQL.

    For the solution i have adapted a database manager for my requirement.

  • Lifetime Qt Champion

    So you're using multiple threads to write to your database ?

  • @SGaist
    Yes. I Import JSON Files with an Import Walter Thread. Wenn a File is copied in a Directory it will Import and created new sql entries.
    Nur Why SQlite has no Troubles?
    I will also Move long Working Stuff to thread

    Ob they GUI you also can do some queries by clicking Buttons

  • Lifetime Qt Champion

    I'd say pure luck.

    Are you creating one connection per thread like suggested in the documentation ?

  • @SGaist
    Yes. The DatabaseManager save the connections per thread into a

    QHash<QThread*, QHash<QString, QSqlDatabase> > 

    I have only one thread at a time wich write to the database. All others max. 4 to only select
    Sorry for my "Austrian English" ;-)

  • Lifetime Qt Champion

    Can you show how your are setting up the connections ?

  • @SGaist

    The first connection is in my first post
    Than for every connection i call

    QSqlDatabase dbc = DatabaseManager::database("CN");
    QSqlQuery query(dbc)


    #include "databasemanager.h"
    #include <QSqlDatabase>
    #include <QMutexLocker>
    #include <QThread>
    #include <QSqlError>
    #include <QDebug>
    QMutex DatabaseManager::s_databaseMutex;
    QHash<QThread*, QHash<QString, QSqlDatabase> > DatabaseManager::s_instances;
    QSqlDatabase DatabaseManager::database(const QString& connectionName)
        QMutexLocker locker(&s_databaseMutex);
        QThread *thread = QThread::currentThread();
        // if we have a connection for this thread, return it
        QHash<QThread*, QHash<QString, QSqlDatabase> >::Iterator it_thread = s_instances.find(thread);
        if (it_thread != s_instances.end()) {
            QHash<QString, QSqlDatabase>::iterator it_conn = it_thread.value().find(connectionName);
            if (it_conn != it_thread.value().end()) {
                QSqlDatabase connection = it_conn.value();
    //            qDebug() << "Function Name: " << Q_FUNC_INFO << " found SQL connection instances Thread: " << thread->currentThreadId() << " Name: " << connectionName;
                if (connection.isValid())
                    return it_conn.value();
        // otherwise, create a new connection for this thread
        QSqlDatabase connection = QSqlDatabase::cloneDatabase(
        // open the database connection
        // initialize the database connection
        if (! {
            // Todo: Exeption Handling
            qCritical() << "Function Name: " << Q_FUNC_INFO << connection.lastError().text();
            return connection;
        qDebug() << "Function Name: " << Q_FUNC_INFO << " new SQL connection instances Thread: " << thread->currentThreadId() << " Name: " << connectionName;
        s_instances[thread][connectionName] = connection;
        return connection;
    #include "qrkcore_global.h"
    #include <QMutex>
    #include <QHash>
    #include <QSqlDatabase>
    class QThread;
    class QRK_EXPORT DatabaseManager
            static QSqlDatabase database(const QString& connectionName = QLatin1String(QSqlDatabase::defaultConnection));
            static QMutex s_databaseMutex;
            static QHash<QThread*, QHash<QString, QSqlDatabase>> s_instances;

    I found the code and adapted it for my requirement.

  • So. I made some tests with create more than 1000 receipts and do concurrent imports with 10000 Products and do some select. I seems it works now without any troubles localhost and IP also over slow connection over the internet.

    i think it fix my problem.
    lg chris

Log in to reply