·ChinaUnix首页 ·论坛 ·博客 
Linux首页 | Linux新闻 | Linux文档 | Linux论坛 | Linux下载 | Linux博客 | Linux搜索
新手入门 | 安装启动 | 管理员指南 | 开发手册 | 桌面应用 | 程序开发 | 数据库 | 网络技术| CentOS | Fedora | MySQL | Apache | Ubuntu | Gentoo| OSCON08
  Linux时代 >> 技术文档 >> 数据库
 
error:QtSql:No such file or directory
来源: ChinaUnix博客  日期: 2008.05.14 10:34 (共有条评论) 我要评论
 

                I wrote a program to test that how to connect to mysql with qt4,but I got the error message as follows when I compile it:
error: QtSql: No such file or directory
There are two method to solve this problem.
1.After execute qmake -project ,edit .pro file and add QT variable definition.
QT += sql
2.edit qmake configuration file,append QT variable definition
Just find the line like this:
QT                      += core gui network
change it :
QT                             += core gui network sql
A example demonstrates  how to connect mysql with qt
//test.cpp
#include
#include
int main(int argc,char **argv)
{
QApplication app(argc,argv);
QTextEdit textEdit;
textEdit.resize(300,300);
textEdit.show();
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("example");
db.setUserName("creatory");
db.setPassword("");
bool ok=db.open();
if(ok)
{
textEdit.setText("connect ok");
QSqlQuery query;
if(query.exec("select * from employee"))
{
int numRows=0;
if(db.driver()->hasFeature(QSqlDriver::QuerySize))
numRows=query.size();
else
{
query.last();
numRows=query.at()+1;
}
QString id,name;
textEdit.append("========");
textEdit.append(QString::fromLocal8Bit("id | Name"));
while(query.next())
{
id=query.value(0).toString();
name=QString::fromLocal8Bit(query.value(1).toByteArray());
QString result=id+" "+name;
textEdit.append(result);
}
textEdit.append(QString("Totally %1 rows").arg(numRows));
}
}
else
{
textEdit.setText("error");
textEdit.append("can't open database");
textEdit.append("Reason:"+db.lastError().databaseText());
}
return app.exec();
}
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/56374/showart_687507.html
  发表评论 查看评论(共有条评论)
 
 


最新资讯更多>> 
· 番茄花园事件给我们的反思
· 红帽收购Qumranet加强虚拟化领..
· Debian 下一个稳定版的代号被确..
· 发行版发布:Mandriva Linux 2..
· Sun VirtualBox v2.0 发布
· Ubuntu Linux用户突破800万
· Mandriva Linux 2009 RC 1 发布
· 构建Linux版本的谷歌Chrome浏览器
· 发行版发布:Scientific Linux..
· BSD发行版:DragonFly BSD 2.1..
论坛热点更多>> 
· 奔腾老机器16M内存,还能用什..
· 【经典】我是一条内存
· 刚接到通知
· 建议!
· 下班我们部门KTV有美女陪 o(..
· 中秋都有什么打算啊
· 誰懂裝修啊,來幫幫忙
· UNIX环境高级编程这本书怎么样?
· 真实的战争~~~~来晚的已经没..
· 《深入理解Linux网络内幕》原..
文档更新更多>> 
· 用YUM升级CentOS系统中PHP和MySQL
· [转]Red Hat Linux 安全设置指南
· 使用Emacs作为文件管理器
· linux内核的移植与遭遇问题的解决
· KStar 校园网锐捷认证GUI版
· 给eclipse安装CDT插件开发C/C++工程
· ubuntu 8.10 安装 Lotus Symphony
· fsck命令
· RHEL5的kickstart中的%packages部..
· linux 线程实现机制分析
 
关于我们 | 联系方式 | 广告合作 | 诚聘英才 | 网站地图 | 友情链接 | 免费注册

Copyright © 2001-2008 ChinaUnix.net All Rights Reserved

感谢所有关心和支持过ChinaUnix的朋友们