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. QSql Query over multiple database
Qt 6.11 is out! See what's new in the release blog

QSql Query over multiple database

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 1.8k Views 2 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.
  • A Offline
    A Offline
    Ankit.Jain
    wrote on last edited by
    #1

    Hi,
    I wish to run my query after selecting the database I wish to run it for. May I be suggested how?

    Ankit

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Do you mean over multiple databases from different database systems or do you mean
      one or more of say SQLite databases?
      Anyway, you can use

      fileName = QFileDialog::getOpenFileName(this, "Open DB", "/home/",     
          "");
      

      To simply point to db file and then u can open it and run the query.

      If all DB files are stored in same folder, you could also use
      QDir to enumerate all files and show in list and pick one from that.

      So many ways to "select db" but it really depends on what u had in mind.

      1 Reply Last reply
      3
      • R Offline
        R Offline
        runnynose12
        wrote last edited by
        #3

        My app has multiple databases, each has its own SQLite connection. Currently, for a single DB, it queries via a QSqlQueryModel subclass (ExecQueryJob runs SQL on a thread pool, then setQuery() binds the live cursor to the model), and displays through a QSortFilterProxyModel + QTreeView.

        I want to query N selected DBs at once and show combined results. My idea was: run one query job per DB, collect all rows into memory, dedupe/sort them myself, then re-insert everything into a synthetic in-memory SQLite table and setQuery() against that, so the model stays a QSqlQueryModel.

        Is there any other way to achieve this?

        SGaistS 1 Reply Last reply
        0
        • R runnynose12

          My app has multiple databases, each has its own SQLite connection. Currently, for a single DB, it queries via a QSqlQueryModel subclass (ExecQueryJob runs SQL on a thread pool, then setQuery() binds the live cursor to the model), and displays through a QSortFilterProxyModel + QTreeView.

          I want to query N selected DBs at once and show combined results. My idea was: run one query job per DB, collect all rows into memory, dedupe/sort them myself, then re-insert everything into a synthetic in-memory SQLite table and setQuery() against that, so the model stays a QSqlQueryModel.

          Is there any other way to achieve this?

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote last edited by
          #4

          @runnynose12 hi and welcome to devnet,

          I think your idea is not bad.
          That said, if you only want to show a one table result, you could use a QStandardItemModel into which you put the data you filtered. That's one layer of complexity removed.

          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
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote last edited by
            #5

            See also this for a SQLite specific solution: https://stackoverflow.com/questions/6824717/sqlite-how-do-you-join-tables-from-different-databases

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

            1 Reply Last reply
            1
            • R Offline
              R Offline
              runnynose12
              wrote last edited by
              #6

              how about instantiating one QSqlQueryModel per DB (each keeps its own connection/cursor, untouched) and combine them at the view layer using QConcatenateTablesProxyModel instead?

              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