QT5.9 connection MySQL5.7 solve QSqlDatabase: QMYSQL driver not loaded question

When using qt mysql database connection, always appears QSqlDatabase: QMYSQL driver not loaded in question. There are many online solutions, more trouble is to recompile the QT mysql drivers qsalmysql.dll, there is a simple method is to copy libmysql.dll to the specified directory (but many did not make it clear). After several attempts to find a final solution to the simplest solution to the problem.

Ready to work

Download qt-opensource-windows-x86-5.9.1.exe
Download mysql-installer-community-5.7.26.0.msi

installation

Since the 32-bit version of qt, so when installing MySQL, select MySQL Server x86 and MySQL Connector / C x86, otherwise there will be QSqlDatabase: QMYSQL driver not loaded in question.

Configuration

Open C: \ Program Files (x86) \ MySQL \ MySQL Connector C 6.1 \ lib copy libmysql.dll and libmysql.lib two files to C: \ Programs \ Qt \ Qt5.9.1 \ 5.9.1 \ mingw53_32 \ bin directory under.

Test code

#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlError>
#include <QtDebug>

void connect_mysql()
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");                //连接数据库主机名
    db.setPort(3306);                                   //连接数据库端口号
    db.setDatabaseName("xxxxxx");           //连接数据库名
    db.setUserName("root");                       //数据库用户名
    db.setPassword("xxxxxx");                    //数据库密码
    db.open();
    if(!db.open())
    {
        qDebug()<<"不能连接"<<"connect to mysql error"<<db.lastError().text();
        return ;
    }
    else
    {
        qDebug()<<"连接成功"<<"connect to mysql OK";
    }
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    connect_mysql();
    return a.exec();
}

Guess you like

Origin www.cnblogs.com/linzijie1998/p/11096167.html