1. 安装qt-opensource-windows-x86-5.12.9 和 mysql 8.0
2. 用qt编译mysql
2.1 修改 mysql.pro
mysql.pro
D:alantop_diralantop_ideQtQt5.12.95.12.9Srcqtbasesrcpluginssqldriversmysql
mysql.pro做如下修改
INCLUDEPATH +="D:alantop_diralantop_datamysql-8.0.20-winx64include"
LIBS +="D:alantop_diralantop_datamysql-8.0.20-winx64liblibmysql.lib"
DESTDIR = ../mysql/lib/
生成的lib库文件:D:alantop_diralantop_ideQtQt5.12.95.12.9Srcqtbasesrcpluginssqldriversmysqllib
2.2 把生成的文件拷贝到对应目录
把libmysql.dll,libmysql.lib添加到QT bin目录下(如:D:alantop_diralantop_ideQtQt5.12.95.12.9mingw73_64in)
把qsqlmysql.dll,qsqlmysqld.dll添加到sqldrivers目录下(如:D:alantop_diralantop_ideQtQt5.12.95.12.9mingw73_64pluginssqldrivers)
3. qt 开发mysql程序
3.1 修改工程文件
QT += core gui sql
3.2 qt连接mysql代码
qDebug() << QSqlDatabase::drivers();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setUserName("test1");
db.setPassword("12345678");
db.setDatabaseName("wealth");
if (!db.open())
{
QMessageBox::warning(this, "fail", db.lastError().text());
return;
}
QSqlQuery query;
query.exec("select name from bankbase");
while (query.next())
{
qDebug() << query.value("name").toString();
}
4. 修改mysql 8.0密码认证方式
CREATE USER test1@'%' IDENTIFIED WITH mysql_native_password BY '12345678'
GRANT ALL on *.* TO test1@'%'