首先得安装MySQL,这个好说。
安装后在qt里建立一个空项目,在.pro里加上 QT+=sql
在项目上新建文件,conn.h 加入下面代码:
#ifndefCONN
#defineCONN
#include
#include
boolconnectiondatabes()
{
QSqlDatabasedb=QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("mysql");
db.setUserName("root");
if(db.open())
{
qDebug()<
}
else
{
qDebug()<
returnfalse;
}
returntrue;
}
#endif//CONN
在main.app中:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
connectiondatabes();
return a.exec();
}
运行 如果显示 build error,那么重点来了.(下面为网上资料,亲测可用)
1配置MYSQL