当前位置:Gxlcms > mysql > qt5-QT使用mysql时出现问题,求大神看下

qt5-QT使用mysql时出现问题,求大神看下

时间:2021-07-01 10:21:17 帮助过:56人阅读

mysqlqt5

我的系统:win7 64bit
QT版本:QT5.4 vs2013的编译器
MySQL5.6安装正常
我是初学者,想试验一下mysql的使用,用main函数调用以下代码时出现:
Starting C:\Qt\Qt5.4.1\QTprojects\build-Mysql_test-Desktop_Qt_5_4_1_MSVC2013_64bit-Debug\debug\Mysql_test.exe...
build error!haha
C:\Qt\Qt5.4.1\QTprojects\build-Mysql_test-Desktop_Qt_5_4_1_MSVC2013_64bit-Debug\debug\Mysql_test.exe exited with code 0

代码如下:
#ifndef CONNECTDATABASE_H
#define CONNECTDATABASE_H

#include
#include
#include
#include
#include
#include
bool connectiondatabase()
{
QSqlDatabase db(QSqlDatabase::addDatabase("QMYSQL"));
db.setHostName("localhost");
db.setDatabaseName("Student");
db.setUserName("root");
db.setPassword("123456");
if(db.open())
{
qDebug()<<"database is established!";
}
else
{
qDebug()<<"build error!haha";
return false;
}
QSqlQuery query;

query.exec(QObject::tr("create table student(sno varchar(10) not null primary key,sname "                       "varchar(20) not null,scalss varchar(10) not null"));query.exec(QObject::tr("insert into student(sno,sname,sclass) values('2009001','lilei',"                       "'0901')"));query.exec(QObject::tr("insert into student(sno,sname,sclass) values('2009002','lucy',"                       "'0902')"));query.exec(QObject::tr("insert into student(sno,sname,sclass) values('2009003','mike',"                       "'0903')"));query.exec("select * from student");qDebug()<<"sno     sname      scalss";while(query.next()){    qDebug()<

}

#endif // CONNECTDATABASE_H

人气教程排行