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. Multiple Sql drivers and database connections in one application

Multiple Sql drivers and database connections in one application

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlsqliteoraclemultiple
3 Posts 2 Posters 5.2k 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.
  • VagabondV Offline
    VagabondV Offline
    Vagabond
    wrote on last edited by
    #1

    My application is connected to an oracle database. A new feature requires exporting a subsection of the database to be exported as a local database, so the user can work offline. For this task I have decided to introduce Sqlite into the app.
    I have setup an export feature, which will:


    1.) connect to the oracle db

    oci_db_ = QSqlDatabase::addDatabase("QOCI");
    

    2.) pull info about a table

    QSqlQuery qry(oci_db_);
    

    3.) store it in a container

        QList< QList<QString> > results;
        if (oci_db_.isOpen()) {
            QSqlQuery qry(oci_db_);
            qry.setForwardOnly(true);
            if(qry.exec(queryString)) {
                while(qry.next()) {
                    QList<QString> row;
                    results.append(row);
                    for(int i = 0; i < qry.record().count(); ++i) {
                        results.last().append(qry.value(i).toString());
                    }
                }
            } else {
                qDebug() << queryString;
                qDebug() << "FAILURE: cannot handle query.";
                qDebug() << "  > " << qry.lastError();
            }
        } else {
            qDebug() << "Error Opening Database = " << oci_db_.lastError();
        }
    

    4.) close the oracle db

    oci_db_->close()
    

    5.) open an sqlite connection (add if it does not already exist)

    sqlite_db_ = QSqlDatabase::addDatabase("QSQLITE");
    

    6.) create tables based on pulled data

    QSqlQuery qry(sqlite_db_);
    qry.exec("CREATE TABLE ...");
    

    7.) close sqlite connection

    sqlite_db_->close()
    

    8.) back to 1.) until all tables that need to be stored are retrieved


    The Sqlite driver is found. When I add the database I get:

    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

    Everytime I then try to open and use the Sqlite connection I get this Sql error:

    QSqlError("", "Driver not loaded", "Driver not loaded")

    Before I throw myself into a time consuming bug fixing session, I was wondering, is Qt even able to setup multiple database connections across different platforms in one session? Any other suggestions that would direct my journey on finding the problem are really appreciated, too.

    1 Reply Last reply
    0
    • kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @Vagabond
      Hello,
      You could name your connections and not close them every time, e.g.:

      QSqlDatabase::addDatabase("QOCI", "myOracle");
      QSqlDatabase::addDatabase("QSQLITE", "mySQLite");
      

      Then if you need any of those two in another part of your program, you can simply get them by their name:

      QSqlDatabase oci = QSqlDatabase::database("myOracle");
      

      Kind regards.

      Read and abide by the Qt Code of Conduct

      VagabondV 1 Reply Last reply
      2
      • kshegunovK kshegunov

        @Vagabond
        Hello,
        You could name your connections and not close them every time, e.g.:

        QSqlDatabase::addDatabase("QOCI", "myOracle");
        QSqlDatabase::addDatabase("QSQLITE", "mySQLite");
        

        Then if you need any of those two in another part of your program, you can simply get them by their name:

        QSqlDatabase oci = QSqlDatabase::database("myOracle");
        

        Kind regards.

        VagabondV Offline
        VagabondV Offline
        Vagabond
        wrote on last edited by
        #3

        @kshegunov Thanks I will try that.

        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