Dr Deo
Dr Deo

Reputation: 4838

QT nightmare with 2 simultaneous database connections

I need to read from a microsoft ODBC database and a postgres database at the same time in my QT code.

Problems

  1. Once i open up the database connections, how do i instruct qsqlQuery which one to use?
  2. why are my database connections failing yet these databases exist
Thanks

#include <QtCore/QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <Qdebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //odbc
    QSqlDatabase dbODBC= QSqlDatabase::addDatabase("QODBC","microsoft_connection");
    dbODBC.setDatabaseName("BIO");
    if(!dbODBC.open())
    {
        qDebug()<<"failed to open BIO";
        exit(1);
    }
    //postgress
    QSqlDatabase dbPostgres= QSqlDatabase::addDatabase("QPSQL","postgres_connection");
    dbPostgres.setDatabaseName("makerere_boys_db");
    dbPostgres.setHostName("127.0.0.1");
    dbPostgres.setUserName("postgres");
    dbPostgres.setPassword("student");  
    if (!dbPostgres.open()) {
        qDebug()<<"failed to open postgres database";
        exit(1);
    }
    //how do i tell QSqlQuery to use dbODBC instead of dbPostgress?. Frustration follows
    QSqlQuery query;
    query.exec("SELECT * FROM fees");
    qDebug()<<query.value(0).toString();
    return a.exec();
    system("pause");
}

The above code compiles but QSqlQuery says database is not open

Upvotes: 1

Views: 699

Answers (3)

dschulz
dschulz

Reputation: 4796

QSqlQuery query1(QSqlDatabase::database("postgres_connection"));
query1.exec("SELECT * FROM fees");
while (query1.next()){
    QString col0 = query1.value(0).toString();
    QString col1 = query1.value(1).toString();
    qDebug() <<  QString("%1 , %2").arg(col0).arg(col1);
}



QSqlQuery query2(QSqlDatabase::database("microsoft_connection"));
query2.exec("SELECT * FROM fees");    
while (query2.next()){
    QString col0 = query2.value(0).toString();
    QString col1 = query2.value(1).toString();
    qDebug() <<  QString("%1 , %2").arg(col0).arg(col1);
}

Kinda off-topic, but you don't really need to instantiate a QCoreApplication (or QApplication) to run queries against a RDBMS. You can safely comment out

// this line
#include <QtCore/QCoreApplication>
// and this line
QCoreApplication a(argc, argv);
// and this line
return a.exec();

Upvotes: 1

dag
dag

Reputation: 2218

For issue #1: You need to pass the database in as an argument to the QSqlQuery constructor:

QSqlQuery query(dbPostgres);
...

For issue #2: Look at the documentation for the QSqlDatabase class. At the bottom of the function description for addDatabase it states:

Before using the connection, it must be initialized. e.g., call some or all of setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), and setConnectOptions(), and, finally, open().

Looks like you are only calling setDatabaseName. You may need to provide the object with the additional information described.

Upvotes: 3

user124493
user124493

Reputation:

Notice, you are using this ctor for your query object, and just using the default arguments for both parameters. Notice what the docs say. You are therefore telling the ctor to use the default database, which must be the postgres one for you.

Upvotes: 3

Related Questions