问题描述
- QT使用 mysql时出现问题,求大神看下
-
我的系统:win7 64bit
QT版本:QT5.4 vs2013的编译器
MySQL5.6安装正常
我是初学者,想试验一下mysql的使用,用main函数调用以下代码时出现:
Starting C:QtQt5.4.1QTprojectsbuild-Mysql_test-Desktop_Qt_5_4_1_MSVC2013_64bit-DebugdebugMysql_test.exe...
build error!haha
C:QtQt5.4.1QTprojectsbuild-Mysql_test-Desktop_Qt_5_4_1_MSVC2013_64bit-DebugdebugMysql_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
解决方案
db打开失败
1.目录环境中有mysql的驱动吗?
2.可以安装navicat for mysql 看看能连接成功不
解决方案二:
有驱动,应该是这个吧,我试试navicat for mysql ,谢谢!
解决方案三:
qt 的pro 加了qt+=sql么
解决方案四:
请问解决了吗?我刚好现在遇到跟你一模一样的问题
时间: 2024-11-01 10:02:41