Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems with using QT sqlite
Forum Updated to NodeBB v4.3 + New Features

Problems with using QT sqlite

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    coderKnight
    wrote on last edited by
    #1

    I used QT sqlite database function with QT version 5.9.9 early,but ,for some reasons I migrate my program to QT version 5.14.2. Some strange things happened. I create a global object to excute such as "select,insert,update ..." operattions and return to the caller a local object eigher QSqlQuery or QSqlRecord,the caller use a local object to stored the object, the procedure like:

    QSqlQuery ExecuteSqlQuery(QString sql){
    QSqlQuery result;
    //........
    return result;
    }

    caller:
    void queryData(void){
    QSqlQuery result;
    result = ExecuteSqlQuery("select * from table_driver");
    }

    when I try query data from database in multithread with QT5.14.2,segment fault is catched.but it is run well with QT 5.9.9,.
    Is there any diffrent between the two versions?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by ChrisW67
      #5

      Yes,I agree with you,but what make me confused is that the code run well witch version 5.9.9,

      A little thing called luck. All it takes is a difference in class code, thread timing/scheduling and two shared users of the object do, or do not, stand on each others toes.

      As @Sgaist said, you need to call have a connection created and maintained in each thread that will use the database. That means using the connectionName parameter in your QSqlDatabase::addDatabase() call in each thread to identify different connections. Your arrangement of a free function to create and execute a QSqlQuery needs to change to create the QSqlQuery object using the correct QSqlDatabase instance and, probably, must also be executed in the correct thread.

      C 1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #2

        @coderKnight said in Problems with using QT sqlite:

        when I try query data from database in multithread with QT5.14.2,segment fault is catched.but it is run well with QT 5.9.9,.
        Is there any diffrent between the two versions?

        Nothing of substance. You are quite probably falling foul of undefined behaviour when using QSqlDatabase in a multi-threaded environment.
        Threads and the SQL Module

        C 1 Reply Last reply
        2
        • C ChrisW67

          @coderKnight said in Problems with using QT sqlite:

          when I try query data from database in multithread with QT5.14.2,segment fault is catched.but it is run well with QT 5.9.9,.
          Is there any diffrent between the two versions?

          Nothing of substance. You are quite probably falling foul of undefined behaviour when using QSqlDatabase in a multi-threaded environment.
          Threads and the SQL Module

          C Offline
          C Offline
          coderKnight
          wrote on last edited by
          #3

          @ChrisW67
          Yes,I agree with you,but what make me confused is that the code run well witch version 5.9.9, perhaps I should set a lock when operate database ,because it behaved as if the QSqlDatabase returned a global object to me,if I do other operation in other thread,I make the global object dirty,I will try and post the test result here

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Hi,

            That's why you should use one different connection to your database per thread. Create said connection in the thread that will use it.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by ChrisW67
              #5

              Yes,I agree with you,but what make me confused is that the code run well witch version 5.9.9,

              A little thing called luck. All it takes is a difference in class code, thread timing/scheduling and two shared users of the object do, or do not, stand on each others toes.

              As @Sgaist said, you need to call have a connection created and maintained in each thread that will use the database. That means using the connectionName parameter in your QSqlDatabase::addDatabase() call in each thread to identify different connections. Your arrangement of a free function to create and execute a QSqlQuery needs to change to create the QSqlQuery object using the correct QSqlDatabase instance and, probably, must also be executed in the correct thread.

              C 1 Reply Last reply
              0
              • C ChrisW67

                Yes,I agree with you,but what make me confused is that the code run well witch version 5.9.9,

                A little thing called luck. All it takes is a difference in class code, thread timing/scheduling and two shared users of the object do, or do not, stand on each others toes.

                As @Sgaist said, you need to call have a connection created and maintained in each thread that will use the database. That means using the connectionName parameter in your QSqlDatabase::addDatabase() call in each thread to identify different connections. Your arrangement of a free function to create and execute a QSqlQuery needs to change to create the QSqlQuery object using the correct QSqlDatabase instance and, probably, must also be executed in the correct thread.

                C Offline
                C Offline
                coderKnight
                wrote on last edited by
                #6

                @ChrisW67
                Sorry back to you late, I got into trouble for another item. If I create diffrent connection for diffrent thread ,I have too much code to be modified,so I set a mutex lock before operate the database ,I have tested for two days, it runs well.But I use the old code to do some test by the version5.9.9 again and again,it still works well,I don't know the reason,
                Thanks for all

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  It's because of this change: https://code.qt.io/cgit/qt/qtbase.git/commit/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp?id=610a9e8f319eafcbcf193e4b90119a6f89d0caf9

                  You must not use a QSqlDatabase connection in more than one thread (and therefore this global sqlite lock was removed for performance reasons).

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  C 1 Reply Last reply
                  3
                  • Christian EhrlicherC Christian Ehrlicher

                    It's because of this change: https://code.qt.io/cgit/qt/qtbase.git/commit/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp?id=610a9e8f319eafcbcf193e4b90119a6f89d0caf9

                    You must not use a QSqlDatabase connection in more than one thread (and therefore this global sqlite lock was removed for performance reasons).

                    C Offline
                    C Offline
                    coderKnight
                    wrote on last edited by
                    #8

                    @Christian-Ehrlicher
                    Thank you so much for let me know the reason

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved