Qt MySQL connection problem solution

Qt5 there will be some problems connecting to MySQL database, this article describes the two most common problems, as well as its relatively simple solution.

Qt5 MySQL database does not support list

Enter the following code to view the supported database types

//打印支持的数据库类型
qDebug()<<QSqlDatabase::drivers();

phenomenon:

Support can be found in the database does not list "QMYSQL", open Qt5.X.X\5.X.X\mingwXX_XX\plugins\sqldriversmay find the lack of the following two files:

The reason: an earlier version of Qt5 is not with the MySQL driver, that is not qsqlmysql.dll, and qsqlmysqld.dllthese two files, you need to manually add the compiler.

Solution 1: Starting Qt5.8 (specifically to be verified) comes after the default MySQL driven (currently the fastest update 5.1.4 is not with the MySQL driver), so we can upgrade themselves to more than 5.8 version, the problem can be solved, it is recommended that long-term support version 5.9.

Solution 2: to manually compile driver files or download the corresponding drive others compiled files added to it. Compiled refer to the official web sql-driver

QMYSQL drive not loaded

Enter the following code database connection

//连接数据库
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setUserName("root");
db.setPassword("hll523811");
db.setDatabaseName("db");
if(db.open()==false){
    qDebug()<<"打开失败"<<db.lastError().text();
}

phenomenon:

Classic problems arise: QMYSQL drive not loaded

The reason:Qt5.X.X\5.X.X\mingwXX_XX\bin\ lack of MySQL lib file, so we can not use

Solution: Open the mysql-X.X.XX-winxXX\libcopy libmysql.dllto the Qt5.X.X\5.X.X\mingwXX_XX\bin\can

note:

1. The libmysql.dlldocument must match the appropriate use of MySQL version

2.32 QT replication is 32 bits in MySQL libmysql.dll, but also the corresponding 64-bit 64-bit

3. Different versions of MySQL which contains different libmysql.dll, different libmysql.dllneed and supporting qsqlmysqld.dll qsqlmysql.dlltogether to work

Guess you like

Origin www.cnblogs.com/hanlulu1998/p/12597903.html