Problem using QSqlTableModel with several sqlite databases
-
I am trying to get info about the content of certain table in several .sqlite databases.
Using a simple QtWidget, in wich a combo box has been placed, I select the .sqlite file
I want to check and try to retrieve the data inside the selected file, showing its contents
in a TableView I have placed in the same Widget.
Am using the "activated" signal of the combo box, so every time I select a different file
(meanning, every time I change the item selected in the combo box) the TableView gets updated,
or at least is what am trying to do, using the following snippet:void Bar::on_Foo_Selector_Combo_activated(const QString &arg1) { { QSqlDatabase current_db = QSqlDatabase::addDatabase("QSQLITE"); //ver1 //QSqlDatabase current_db = QSqlDatabase::addDatabase("QSQLITE", "my_DB"); //ver2 current_db.setDatabaseName(arg1); if ( !current_db.open() ) std::cerr<<"ERROR: SQLITE_DB FAILED WHEN OPENNING SELECTED FILE" << endl; QSqlTableModel* my_data_model = new QSqlTableModel(); my_data_model->setTable("TEST_TABLE"); my_data_model->select(); my_data_model->setEditStrategy(QSqlTableModel::OnManualSubmit); Bar->data_table_widget->setModel (my_data_model); } QSqlDatabase::removeDatabase(QSqlDatabase::database().connectionName ()); //ver1 // QSqlDatabase::removeDatabase("my_DB"); //ver2 }
Following Qt documentation recommendations, I have placed the brackets to being able of safe closing and deleting
the database connection after finish using it.So, this is what I get:
When maintanning the code as it is showed (what I call version 1) I obtain the TableView in my widget populated
with the data it must show, but the following error appears anytime this snippet is executed:QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
When uncommenting the lines marked with //ver2 at the line end, and commenting those corresponding to //ver1, then I don't get the previous warning, but I don't get the TableView populated.
What can I do to stop seeing the mentioned warning but still being able to populate the TableView? What am I missing?
-
@adabreug94 said in Problem using QSqlTableModel with several sqlite databases:
Following Qt documentation recommendations, I have placed the brackets to being able of safe closing and deleting
the database connection after finish using it.But you did not finish using it - the QSqlQueryModel still has an instance on it.
I would suggest simply using different connection names for the different connections.