How to load ODBC in qt

asdasdasdasdqwe picture asdasdasdasdqwe · Nov 15, 2011 · Viewed 7.4k times · Source

I'm very new to Qt and C++

I'm trying to connect to an ODBC db, but I'm getting the following output

QSqlDatabase: QODBC driver not loaded
QSqlDatabase: available drivers: QSQLITE 

How do I load ODBC in Qt.

CODE :

QString serverName = "LOCALHOST\\SQLEXPRESS";
QString dbName = "test";

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");

db.setConnectionOptions();

QString myCon = QString("DRIVER={SQL Native Client};SERVER=%1;DATABASE=%2;Trusted_Connection = Yes").arg(serverName).arg(dbName);

db.setDatabaseName(myCon);

if(db.open())
{
   qDebug() << "Opened!";
   db.close();
}
else
{
   qDebug() << "Error : " << db.lastError().text();
}

I tried to follow the steps online, but it's very confusing.

Can someone give me a simplified approach to doing this?

Answer

Tim Meyer picture Tim Meyer · Nov 16, 2011

You will have to manually build the ODBC plugin if you haven't done so already.

This link describes how to build it (Assuming you're on Windows. If not, scroll up a bit.)

In case you already built it or this step didn't work, create a directory in your project called "sqldrivers" and copy the qsqlodbc.dll from the Qt directory into it.