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. Problem using QSqlTableModel with several sqlite databases
Forum Updated to NodeBB v4.3 + New Features

Problem using QSqlTableModel with several sqlite databases

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqltablemodel
2 Posts 2 Posters 300 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.
  • A Offline
    A Offline
    adabreug94
    wrote on last edited by
    #1

    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?

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

      @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.

      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

      • Login

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