[Qt] [QMYSQL]已部署的应用 - 未加载驱动程序

时间:2015-10-15 19:14:12

标签: mysql windows qt

首先,非常感谢那些需要时间帮助我解决这个问题的人。在发布之前,我在很多不同的论坛上搜索了很多,但似乎我错过了一些东西。

好吧,我正在使用Qt5.5 / MySQL Server 5.6处理Windows 7(64位)。 我在Qt Creator上使用MinGW 5.5.0 32位(自动检测)。 这不是构建驱动程序的问题,它已经完成,它对开发人员来说非常有效! :-) 我可以访问我的BD,执行我想要的任何查询并检索/插入所有数据。

我面临的问题是在其他计算机上部署我的应用程序。 我知道我必须将 qsqlmysql.dll 放在我的应用程序中的“ sqldrivers ”文件夹中。目录。例如将 libmysql.dll 放在此目录中。

所以我有类似下面的内容

  • App目录
    • APP.EXE
    • 的libmysql.dll
    • Qt5Core.dll
    • Qt5Gui
    • Qt5Sql
    • Qt5Widget
    • libwinpthread-1.DLL
    • 的libstdc ++ - 6.dll
    • libgcc_s_dw2-1.dll
    • 平台
      • qwindow.dll
    • sqldrivers
      • qsqlmysql.dll

但是当我发布应用程序并尝试从我以前开发的另一台计算机上运行它时,我有一个“未加载驱动程序”错误...

到目前为止,我真的不知道我错过了什么...... 所以,如果有人能给我一些,那真的很感激!

我告诉你代码中真正有用的部分,以防万一...

的main.cpp

QApplication a(argc, argv);
Maintenance w;
w.show();
return a.exec();

Maintenance.cpp

void Maintenance::login(){
    int db_select = 1;
    this->maint_db = Database(db_select);

    /* All that follow is linked to the login of user...  */
}

Database.cpp

Database::Database(int default_db)
{    
    this->db = QSqlDatabase::addDatabase("QMYSQL");
    switch(default_db){
        case 0:
            this->db.setHostName("XXX.XX.XXX.XX");
            this->db.setDatabaseName("maintenance_db");
            this->db.setUserName("USERNAME");
            this->db.setPassword("PASSWORD");
            this->db.setPort(3306);
        break;

        // Only to make some trials in local
        case 1:
            this->db.setHostName("127.0.0.1");
            this->db.setDatabaseName("maintenance_db");
            this->db.setUserName("USERNAME");
            this->db.setPassword("PASSWORD");
        break;
    }    

/* I've added the following code to try to solve the problem

I retrieve that the available drivers are: QMYSQL / QMYSQL3
But all the information about the DB are empty (due to the unloaded driver I assume.)
And the error from *lastError()* is "Driver not loaded"
 */

QString my_drivers;
for(int i = 0; i < QSqlDatabase::drivers().length(); i++){
    my_drivers = my_drivers + " / " + QSqlDatabase::drivers().at(i);
}
QString lib_path;
for(int i = 0; i < QApplication::libraryPaths().length(); i++){
    lib_path = lib_path + " / " + QApplication::libraryPaths().at(i);
}

 QString start = QString::number(QCoreApplication::startingUp());
 QMessageBox::information(0, "BDD init", 
"Drivers available:  " + my_drivers 
+ " \nHostname:  " + this->db.hostName() 
+ "\nDB name:  " + this->db.databaseName() 
+ "\nUsername:  " + this->db.userName() 
+ "\nPW:  " + this->db.password()
+ "\n\n" + lib_path + "\n" + start
);    

if(this->db.isOpen()){
    QMessageBox::information(0, "BDD init", "Already open.");
}
else{
    if(this->db.open()){
        QMessageBox::information(0, "BDD init", "Opened.");
    }
    else{
        QMessageBox::critical(0, "BDD init", "Not opened.\n" + this->db.lastError().text());
    }
}
}

2 个答案:

答案 0 :(得分:1)

至少有3种可能的解决方案:

  1. 使用您最喜欢的进程监视器查找所有.dll路径是否正确
  2. 确保所有.dll与.exe相同,即x86(32位)
  3. 使用QPluginLoader
  4. 进行调试

答案 1 :(得分:0)

为您创建“部署”文件夹的最简单方法Qt5应用程序正在使用windeployqt工具

创建空目录,复制app.exe文件,然后运行windeployqt app.exe

查看文档http://doc.qt.io/qt-5/windows-deployment.html#the-windows-deployment-tool

相关问题