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. How to switch SQL Database at runtime with model.
Forum Update on Monday, May 27th 2025

How to switch SQL Database at runtime with model.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 249 Views
  • 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.
  • S Offline
    S Offline
    sandro4912
    wrote on 30 Nov 2020, 19:55 last edited by sandro4912
    #1

    So I connect to a database like this:

    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("database.db");
    db.open();
    
    QSqlTableModel model;
    

    This works fine the model points to the db connection. which is the default connection. See the constructor here:

    QSqlTableModel::QSqlTableModel(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase())
    

    Now for some reason I want to set the database later in the program. So I tried this:

    db = QSqlDatabase::addDatabase("QSQLITE");
    
    QSqlTableModel model;
    
    db.setDatabaseName("database.db");
    db.open();
    

    This does not work. If I check directly after model:

    qDebug() << db.isOpen()
    

    It returns true. The db is open.

    I also tried then:

    db = QSqlDatabase::addDatabase("QSQLITE");
    
    QSqlTableModel model;
    
    db.close()
    db.setDatabaseName("database.db");
    db.open();
    

    But still the model does not show anything from the db.

    So Short question:

    I have a model in my app which points to a SQLDatabase. Do I have to create a new model when I want to switch to annother database?

    Do I have to destroy the whole model and create a new one in its place?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 30 Nov 2020, 21:16 last edited by
      #2

      Hi,

      You can also try to call setQuery passing the new database connection.

      But in any case, QSqlTableModel is meant to be used with an established connection.

      Otherwise you should go with the base class and reset the query when changing your database. Depending on what you do, you should consider naming your connections and be explicit about them. It will make your logic easier to follow.

      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
      1
      • S Offline
        S Offline
        sandro4912
        wrote on 30 Nov 2020, 21:30 last edited by sandro4912
        #3

        The problem is I also use this model in qml.

        So that means in QML ever time I change the database I have to create a new model?

        I was thinking about swapping the new model with Loader in QML.

        Also in reality it is not directly QSQLModel. It is a class derived from it defining already a specific format for the tables. So setQuery is not an option in this case-

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 1 Dec 2020, 19:22 last edited by
          #4

          Can you explain why you need to switch to different databases ?

          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

          1/4

          30 Nov 2020, 19:55

          • Login

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