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. How QSqlDatabase can support many thread to selected or update.
Forum Updated to NodeBB v4.3 + New Features

How QSqlDatabase can support many thread to selected or update.

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 2.5k 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.
  • F Offline
    F Offline
    foxgod
    wrote on 8 Apr 2014, 06:18 last edited by
    #1

    when a new socket comes, i create a thread ,every thread create a database object to execute selected or update ,but sometimes program can breakdown or no right result.

    the example ,here

    http://idas643.blog.163.com/blog/static/167104838201331635813904/

    3Q

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 Apr 2014, 19:13 last edited by
      #2

      Hi,

      From a quick overlook, you are creating a new database connection for every thread using the same name so basically reusing the same connection. Based on "this":http://qt-project.org/doc/qt-5/threads-modules.html#threads-and-the-sql-module it's not possible.

      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
      0
      • F Offline
        F Offline
        foxgod
        wrote on 9 Apr 2014, 01:17 last edited by
        #3

        [quote author="SGaist" date="1396984407"]Hi,

        From a quick overlook, you are creating a new database connection for every thread using the same name so basically reusing the same connection. Based on "this":http://qt-project.org/doc/qt-5/threads-modules.html#threads-and-the-sql-module it's not possible.[/quote]

        can you tell me a method to support many thread to query database ,
        the result is right.

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qxoz
          wrote on 9 Apr 2014, 03:29 last edited by
          #4

          Maybe "this":https://gitorious.org/multi-threaded-access-to-a-database might help. But i haven't use it myself.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            foxgod
            wrote on 10 Apr 2014, 00:43 last edited by
            #5

            anything else??

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on 10 Apr 2014, 01:40 last edited by
              #6

              Each thread needs to create its own, unique named database object, connect it to the database, and open it. The resulting connections are independent of each other.

              You can use any scheme you like to get a unique name for the connection. You might use a counter, QUuid or the address of the worker object in hex to get a unique name.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                JKSH
                Moderators
                wrote on 10 Apr 2014, 01:42 last edited by JKSH 4 May 2015, 14:50
                #7

                I haven't tried this myself, but I believe you can create multiple connections to the same SQLite database.

                Try this:

                1. Create a QSqlDatabase in each thread. Make sure they all have the same database name but different connection names
                2. When you create a QSqlQuery, make sure you associate it with the QSqlDatabase of that thread.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0

                7/7

                10 Apr 2014, 01:42

                • Login

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