Multiple Sql drivers and database connections in one application



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


  • Qt Champions 2016

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



  • @kshegunov Thanks I will try that.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.