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. QSqlTableModel and database closing problem
Forum Updated to NodeBB v4.3 + New Features

QSqlTableModel and database closing problem

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 4.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.
  • M Offline
    M Offline
    Maptuzu
    wrote on last edited by
    #1

    Hello,

    how can i disconnect the database from the QSqlTableModel, so that i can close the database without getting warning/errors that the database is still in use?

    I have initialized the model
    @QSqlDatabase dbq = QSqlDatabase::database(currentDatabase);
    QStringList tables = dbq.tables();
    tableModel = new QSqlTableModel(0, dbq);
    tableModel->setTable(tables[0]);
    tableModel->select();
    tableView->setModel(tableModel);@

    and added the model to the TableView. The only way I've found is explicitly deleting the model. Is there a better way?

    Code for closing the database:
    @
    QStringList connections = QSqlDatabase::connectionNames();
    if (connections.size() > 0)
    {
    {
    delete tableModel;
    QSqlDatabase dbc = QSqlDatabase::database(currentDatabase);
    dbc.close();
    qDebug() << dbc.lastError();
    }
    QSqlDatabase::removeDatabase(currentDatabase);
    }
    @

    1 Reply Last reply
    0
    • batman.890825B Offline
      batman.890825B Offline
      batman.890825
      wrote on last edited by
      #2

      Usually you dont have to use QSqlDatabase::removeDatabase but since when you create a sqlmodel of any type but since you dont specify a parent when you created the QSqlTableModel the model will still exist when you close the program in that case you should destroy the model manually or do the operations with the database with a code like this:

      {
      QSqlDatabase db = QSqlDatabase::database("sales");
      QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
      }
      // Both "db" and "query" are destroyed because they are out of scope
      QSqlDatabase::removeDatabase("sales"); // correct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Maptuzu
        wrote on last edited by
        #3

        Yes, but i bind the QSqlTableModel to the TableView (display the result). So if I don't want do destroy the TableView, the only way for me is to delete the model manually, right?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chaleng
          wrote on last edited by
          #4

          Try to:
          @tableModel->clear();@

          It clears the model and releases any acquired resource.
          http://qt-project.org/doc/qt-5.0/qtsql/qsqlquerymodel.html#clear

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Maptuzu
            wrote on last edited by
            #5

            With
            @tableModel->clear();@

            the warning/error still occurs.

            QSqlDatabasePrivate::removeDatabase: connection '/home/maptuzu/test.db' is still in use, all queries will cease to work.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chaleng
              wrote on last edited by
              #6

              Try to check if your connection is closed :
              @
              dbc.close();
              qDebug()<<dbc.isOpen();
              @

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Maptuzu
                wrote on last edited by
                #7

                Yes, the database is closed.

                @if (QSqlDatabase::connectionNames().size() > 0)
                {
                {
                tableModel->clear();
                //delete tableModel;
                QSqlDatabase dbc = QSqlDatabase::database(currentDatabase);
                dbc.close();
                qDebug() << dbc.isOpen();
                qDebug() << dbc.lastError();
                }
                QSqlDatabase::removeDatabase(currentDatabase);
                }@

                output:

                @false
                QSqlError(-1, "", "")
                QSqlDatabasePrivate::removeDatabase: connection '/home/maptuzu/test.db' is still in use, all queries will cease to work.@

                1 Reply Last reply
                0
                • batman.890825B Offline
                  batman.890825B Offline
                  batman.890825
                  wrote on last edited by
                  #8

                  Have you tried already to delete it manually? You have to delete it once you dont need it anymore since you dont specify it a parent, or you can also put a parent and change it using setParent

                  1 Reply Last reply
                  0
                  • KyefK Offline
                    KyefK Offline
                    Kyef
                    wrote on last edited by
                    #9

                    Make the parent of the QTableModel to be the class where its used, include the following code in its destructor or just before you remove QSqlDatabase::removeDatabase() line. (I have included both QSqlRelationalTableModel and QSqlTableModel just in case you are using both or more than one type of model). Good luck!

                    foreach(QSqlRelationalTableModel *rmodel, findChildren<QSqlRelationalTableModel *>()) delete rmodel;
                    foreach(QSqlTableModel *tmodel, findChildren<QSqlTableModel *>())  delete tmodel;
                    

                    Kyef Elliot
                    If Can Imagine It...I Can Do It!!

                    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