lirui2514
Mikejo
2015-05-07 02:04

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

  • qt5
  • mysql

我的系统: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()<<query.value(0).toString()<<"   "<<query.value(1).toString()<<"   "
           <<query.value(2).toString();
}
return true;

}

#endif // CONNECTDATABASE_H

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答