How to load ODBC in qt

a 夏天 提交于 2019-12-22 10:13:48

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/8142657/how-to-load-odbc-in-qt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!