QSql Query over multiple database
-
Hi,
I wish to run my query after selecting the database I wish to run it for. May I be suggested how?Ankit
-
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 usefileName = 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.
-
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?
-
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?
@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. -
See also this for a SQLite specific solution: https://stackoverflow.com/questions/6824717/sqlite-how-do-you-join-tables-from-different-databases
-
how about instantiating one QSqlQueryModel per DB (each keeps its own connection/cursor, untouched) and combine them at the view layer using QConcatenateTablesProxyModel instead?