简体   繁体   English

未加载 Qt ODBC 驱动程序

[英]Qt ODBC driver not loaded

I've got the following problem with QODBC driver: QODBC 驱动程序存在以下问题:

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"返回("QSQLITE", "QODBC3", "QODBC") ,但程序没有打开我的数据库,db->open() 返回false并且错误是"Driver not loaded Driver not loaded"

Whats the use of the QSqlDatabase Parameter in your createOdbcConnection-Method?您的 createOdbcConnection-Method 中 QSqlDatabase 参数的用途是什么? I would rather remove it from there, define a QSqlDatabase Object in your Class-Definition:我宁愿从那里删除它,在你的类定义中定义一个 QSqlDatabase 对象:

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.这取决于您在哪里编译并打算使用与 ODBC 相关的 Qt 代码。 I ran into similar issues.我遇到了类似的问题。

My code was working perfectly on Windows, but returning an error when compiled elsewhere (Linux).我的代码在 Windows 上运行良好,但在其他地方(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.在 Linux 上运行编译后的代码时会遇到麻烦,因为驱动程序 libsqlodbc.so 即使存在于 /plugins/sqldriver 目录中,它也依赖于某些必须独立安装的特定库。

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使用此信息在 Linux 上安装 ODBC(如果需要): 在 linux 上安装 odbc 驱动程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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