I need to read from a microsoft ODBC database and a postgres database at the same time in my QT code.
Problems
- Once i open up the database connections, how do i instruct qsqlQuery which one to use?
- 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
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.