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. QSLQITE - use multiple connections SOLVED
Forum Updated to NodeBB v4.3 + New Features

QSLQITE - use multiple connections SOLVED

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

    hi, all,

    I'm trying to open a SQLite-database connection with a specific name (The reason ist, that in my application I have to work with 2 different databases). I tried following code:

    @QString connName = "Connection_to_Working_DB";
    QSqlDatabase connectionDB = QSqlDatabase::addDatabase("QSQLITE", connName);
    connectionDB.setDatabaseName(connString);
    connectionDB.database(connName);
    QString test = connectionDB.connectionName();
    qDebug() << test;
    QSqlQueryModel *model = new QSqlQueryModel();
    model->setQuery("SELECT connection_name FROM tbl_connections");
    ComboBoxConnections->setModel(model);@

    qDebug() gives me the correct connection name, but when I start the query, I get the message "QSqlQuery::exec: database not open"...

    When I try the same code without specifying a connection name, everything works fine...

    can anybody help me?

    thanks in advance,
    Vittorio

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Seba84
      wrote on last edited by
      #2

      You are missing the call to

      @bool QSqlDatabase::open()@

      Try the following:

      @QString connName = "Connection_to_Working_DB";
      QSqlDatabase connectionDB = QSqlDatabase::addDatabase("QSQLITE", connName);
      connectionDB.setDatabaseName(connString);
      if(!connectionDB.open())
      QMessageBox::critical(0, "Cannot open database", connectionDB.lastError().text(), QMessageBox::Ok); @

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vittorio
        wrote on last edited by
        #3

        Unfortunately this doesn't work... :-(

        As I take it, "connectionDB.database(connName)" is an alternative way to open the database, if you want to use a specific connection name, but obviously I'm doing something wrong...

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          It's not an alternative to open a database. It gives you the QDatabase object that represent the connection to a given database (here identified by connName) You still have to open each connection before you can use it.

          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
          • V Offline
            V Offline
            Vittorio
            wrote on last edited by
            #5

            hm, what does this mean?

            @QSqlDatabase connectionDB = QSqlDatabase::addDatabase("QSQLITE", "Test");
            connectionDB.setDatabaseName(connString);
            connectionDB.database("Test");
            connectionDB.open();@

            this code results in the same error, what do I overlook?!?

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jmimi
              wrote on last edited by
              #6

              Pass db connection to setQuery method in line
              @model->setQuery("SELECT connection_name FROM tbl_connections");@
              like
              @model->setQuery("SELECT connection_name FROM tbl_connections", connectionDB);@

              1 Reply Last reply
              0
              • V Offline
                V Offline
                Vittorio
                wrote on last edited by
                #7

                that's it, thanks a lot!!

                1 Reply Last reply
                0

                • Login

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