qtcreator mysql_一步步学Qt.第二天-续集-mysql连接成功

不断辛苦,不断尝试,这把才将mysql的驱动ok了.

写了一个小小的demo

#include

#include

#include

#include

#include

int main(int argc,char *argv[]){

QApplication app(argc,argv);

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

db.setDatabaseName("test");

db.setUserName("root");

db.setPassword("admin");

bool succ = db.open();

if (succ) {

qDebug("yes");

} else {

qDebug("No");

QMessageBox::critical(0,QObject::tr("Error"),db.lastError().text());

}

return app.exec();

}

在写这个程序的时候,忘记了之前在linux中写连接mysql的时候遇到过的问题.

就出现了如下的错误:

debug/main.o: In function `Z5qMainiPPc':

E:\WorkPlace\Qt\dbdemo-build-desktop/../dbdemo/main.cpp:9: undefined reference to `_imp___ZN12QSqlDatabase17defaultConnectionE'

E:\WorkPlace\Qt\dbdemo-build-desktop/../dbdemo/main.cpp:9: undefined reference to `_imp___ZN12QSqlDatabase11addDatabaseERK7QStringS2_'

E:\WorkPlace\Qt\dbdemo-build-desktop/../dbdemo/main.cpp:9: undefined reference to `_imp___ZN12QSqlDatabaseD1Ev'

E:\WorkPlace\Qt\dbdemo-build-desktop/../dbdemo/main.cpp:10: undefined reference to `_imp___ZN12QSqlDatabase15setDatabaseNameERK7QString'

E:\WorkPlace\Qt\dbdemo-build-desktop/../dbdemo/main.cpp:11: undefined reference to `_imp___ZN12QSqlDatabase11setUserNameERK7QString'

E:\WorkPlace\Qt\dbdemo-build-desktop/../dbdemo/main.cpp:12: undefined reference to `_imp___ZN12QSqlDatabase11setPasswordERK7QString'

E:\WorkPlace\Qt\dbdemo-build-desktop/../dbdemo/main.cpp:13: undefined reference to `_imp___ZN12QSqlDatabase4openEv'

E:\WorkPlace\Qt\dbdemo-build-desktop/../dbdemo/main.cpp:21: undefined reference to `_imp___ZN12QSqlDatabaseD1Ev'

E:\WorkPlace\Qt\dbdemo-build-desktop/../dbdemo/main.cpp:21: undefined reference to `_imp___ZN12QSqlDatabaseD1Ev'

collect2: ld returned 1 exit status

就是在.pro文件中加入:

QT += sql

再有我在使用

QMessageBox::critical(0,QObject::tr("Error"),db.lastError().text());的时候,db.lastError()后面的.text()函数,在没有#include 的时候,没有自动提示.我的感觉和认识是没有自动提示,那么很有可能是错误的.但是加入这个之后.就可以了,不过,这头文件,只是所谓的头文件,那是一个目录?

有哪位朋友知道的,还请赐教.小弟感激!

---------------------------------------------

在完成上面的工作之后,

To use the SDK and QtCreator directly, copy these libraries to yourC:\Qt\...\qt\plugins\sqldrivers\, and copyC:\MySQL\MySQL51\lib\opt\libmysql.dllto

yourC:\Qt\...\qt\bin\.

程序运行结果:

Starting E:\WorkPlace\Qt\dbdemo-build-desktop\debug\dbdemo.exe...

yes

oh yeah...今天的任务完成....准备睡觉

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值