dearn44
dearn44

Reputation: 3422

Qt Connection to DB2

As a test I tried connecting to a DB2 server using the QODBC driver. I did this by creating a DSN and then providing the needed data such as hostname and the rest.

But what if I want to run my app in another computer. Is there another way to connect to a DB2 database, because as I see it this would also limit me if I tried to compile and run my program in iOS.

Upvotes: 1

Views: 811

Answers (1)

Leo Chapiro
Leo Chapiro

Reputation: 13984

You can use QSqlDatabase class like this:

bool createConnection()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QDB2");
    db.setHostName("mozart.konkordia.edu");
    db.setDatabaseName("musicdb");
    db.setUserName("gbatstone");
    db.setPassword("T17aV44");
    if (!db.open()) {
        QMessageBox::critical(0, QObject::tr("Database Error"),
                              db.lastError().text());
        return false;
    }
    return true;
}

[EDITED]

How to Build the QDB2 Plugin on Windows

The DB2 header and include files should already be installed in the right directories. You just have to build the plugin as follows:

 cd %QTDIR%\src\plugins\sqldrivers\db2
 qmake "INCLUDEPATH+=<DB2 home>/sqllib/include" "LIBS+=<DB2 home>/sqllib/lib/db2cli.lib"
 nmake

Upvotes: 1

Related Questions