简体   繁体   中英

Qt ODBC driver not loaded

I've got the following problem with QODBC driver:

bool Dialog::createOdbcConnection(QSqlDatabase * db, QString odbcName,QString user,QString pass)
{
    db = new QSqlDatabase();
    db->addDatabase("QODBC");
    db->setDatabaseName(odbcName);
    if(!user.isEmpty())
        db->setUserName(user);
    if(!pass.isEmpty())
        db->setPassword(pass);

    qDebug() << QSqlDatabase :: drivers();

    if (!db->open())
    {
            QMessageBox mgs;
            qDebug() << db->lastError().text();
            mgs.setText(db->lastError().text());
            mgs.exec();
            return false;
    }

    return true;
}

qDebug() << QSqlDatabase :: drivers(); returns ("QSQLITE", "QODBC3", "QODBC") , but the program doesn't open my database, db->open() returns false and the error is "Driver not loaded Driver not loaded"

Whats the use of the QSqlDatabase Parameter in your createOdbcConnection-Method? I would rather remove it from there, define a QSqlDatabase Object in your Class-Definition:

private:
    QSqlDatabase db_;

And initialize it in your Class-Constructor:

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

That should work!

I am seeing this problem is resolved, but I am adding the notes in case someone might be going into the same troubles as mine and looking for a solution when the driver is not loaded, but the described solution is not enough.

It depends on where you compile and intend to use your ODBC-related Qt code. I ran into similar issues.

My code was working perfectly on Windows, but returning an error when compiled elsewhere (Linux).

When running the compiled code on Linux you will get into trouble because the driver, libsqlodbc.so , even if it exists in the /plugins/sqldriver directory , it depends on some particular library which has to be installed independently.

you can see which library is missing by

ldd ./path-to-libsqlodbc.so/libsqlodbc.so

you can see if any other library is missing to make your binary file run by

ldd ./path-to-your-binary-file/name-of-your-binary-file

Use this information to install the ODBC on your Linux (if you need so): installing odbc driver on linux

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM