Qt中数据库查询方法
来源:互联网 发布:全球十大金融公司知乎 编辑:程序博客网 时间:2024/05/14 23:35
Qt使用QSqlQuery来实现数据的查询,QSqlQuery提供了进行SQL语句数据查询,数据处理的用户接口。
QSqlQuery的几个主要方法
1,QSqlQuery
QSqlQuery(const QString &query = QString(),QSqlDatabasedb = QSqlDatabase());QSqlQuery(QSqlDatabasedb);
说明:构造函数
参数:query是查询语句,默认值为空语句,db为使用的数据库连接
返回值:无
例子:
QSqlQuery query("select sname from student",db);QSqlRecord rec = query.record();while(query.next()){ rec = query.record(); int snamecol = rec.indexOf("sname"); QString value = query.value(snamecol).toString(); qDebug()<<"sname:"<<value;}
2,exec()
bool exec();bool exec(const QString &query);
说明:执行默认的或指定的SQL语句
参数:query是指定的SQL查询语句
返回值:执行是否成功.
实例:
QSqlQuery query;query.exec("select sno from student");QSqlRecord rec = query.record();while(query.next()){ rec = query.record(); int snocol = rec.indexOf("sno"); QString value = query.value(snocol).toString(); qDebug()<<"sno:"<<value;}
3,prepare()
bool prepare(const QString &query);
说明:设置将要执行的SQL语句
参数:query是指定的SQL语句
返回值:设置是否成功
实例:
QSqlQuery query;// 设置将要执行的SQL查询语句,:sname是要被绑定数据的位置query.prepare("select * from student where sname = :sname");query.bindValue(":sname","lucy"); // 绑定数据到指定的位置query.exec();QSqlRecord rec = query.record();while(query.next()){ rec = query.record(); int snocol = rec.indexOf("sno"); int snamecol = rec.indexOf("sname"); int sclasscol = rec.indexOf("sclass"); QString value1 = query.value(snocol).toString(); QString value2 = query.value(snamecol).toString(); QString value3 = query.value(sclasscol).toString(); qDebug()<<"sno:"<<value1<<"\t"<<"sname:"<<value2<<"\t"<<"sclass:"<<value3;}
4,bindValue()
void bindValue(const QString &placeholder,const QVariant &val,QSql::ParamTypeparamType=QSql::In);void bindValue(int pos,const QVariant &val,QSql::ParamTypeparamType=QSql::In);
说明:设置将执行的SQL语句要绑定的值
参数:placeholder和pos规定要绑定的位置,val指要绑定的值,paramType默认参数类型为QSql::In
返回值:空
5,first()
bool first();
说明:移动到查询结果的第一条记录
参数:无
返回值:移动是否成功
实例:
QSqlQuery query;query.exec("select sno from student");QSqlRecord rec = query.record();// 移动到第一条语句,如果移动成功则输出此条记录if(query.first()){ rec = query.record(); int snocol = rec.indexOf("sno"); qDebug()<<"sno:"<<query.value(snocol).toString();}
6,last()
bool last();
说明:移动到查询结果的最后一条记录
参数:无
返回值:移动是否成功
7,next()
bool next();
说明:移动到查询结果的下一条记录
参数:无
返回值:移动是否成功
8,previous();
bool previous();
说明:移动到查询结果的上一条记录
参数:无
返回值:移动是否成功
9,seek()
bool seek(int index,bool relative = false);
说明:移动到查询结果的指定记录处
参数:无
返回值:移动是否成功
实例:
QSqlQuery query;query.exec("select sno from student");QSqlRecord rec = query.record();// 移动到查询结果的第二条记录处,如果移动成功则输出记录内容if(query.seek(2)){ rec = query.record(); int snocol = rec.indexOf("sno"); qDebug()<<"sno:"<<query.value(snocol).toString();}
10.clear()
void clear();
说明:清空查询结果并释放系统资源
参数:无
返回值:空
11,isActive()
bool isActive() const;
说明:查看当前查询是否处于激活状态,激活状态指SQL语句是否被运行过
参数:无
返回值:是否处于激活状态
实例:
QSqlQuery query;query.exec("select * from student");if(query.isActive()){ qDebug()<<"this query is active";}else{ qDebug()<<"this query is not active";}
12,isValid()
bool isValid() const;
说明:判断当前记录是否有效
参数:无
返回值:判断结果
实例:
QSqlQuery query;query.exec("select sno from student");if(query.isValid()){ qDebug()<<"Valid record!";}else{ qDebug()<<"Invalid record!";}
13,isNull()
bool isNull(int field) const;
说明:判断当前数据记录中某个列是否为空
参数:列的编号
返回值:是否为空
实例:
QSqlQuery query;query.exec("select sno from student");QSqlRecord rec = query.record();int snocol = rec.indexOf("sno");if(query.first()){ if(query.isNull(snocol)) { qDebug()<<"this field is null"; } else { qDebug()<<"this field is not null"; }}
14,size()
int size() const;
说明:获取查询结果中记录的数目
参数:无
返回值:记录的数目,如果查询失败,则返回-1
实例:
QSqlQuery query;query.exec("select sno from student");int size = query.size(); // 获取查询结果中记录的个数qDebug()<<"the number of the record is:"<<size;
15,value()
QVariant value(int index) const;
说明:获取当前数据记录的某列的数据
参数:列的编号
返回值:数据值,返回值可以使用QVarient中的相应的函数获取由其基本类型表示其值
实例:
QSqlQuery query;query.exec("select * from student");QSqlRecord rec = query.record();while(query.next()){ rec = query.record(); int snocol = rec.indexOf("sno"); int snamecol = rec.indexOf("sname"); int sclasscol = rec.indexOf("sclass"); // 获取当前记录中某一列的值 QString value1 = query.value(snocol).toString(); QString value2 = query.value(snamecol).toString(); QString value3 = query.value(sclasscol).toString(); qDebug()<<"sno:"<<value1<<"\t"<<"sname:"<<value2<<"\t"<<"sclass:"<<value3;}
- Qt中数据库查询方法
- Qt中连接Mysql数据库的方法
- QT 查询数据库
- 查询数据库中重复记录的方法
- 查询数据库中重复记录的方法
- android中数据库查询优化方法
- android中sqlite数据库查询优化方法
- 查询数据库中重复记录的方法
- 查询数据库中重复记录的方法
- 查询数据库中重复记录的方法
- QT 查询数据库 sql QSqlQueryModel
- <job_demo><Qt>数据库简单查询
- Qt将从数据库中查询到的字符串数据输出在自定义的控件中
- 数据库查询提高效率方法
- 数据库分页查询方法
- 数据库分页查询方法
- 数据库分页查询方法
- android 数据库查询方法
- 层次聚类的python实现
- 国际化i18n流程汇总图解
- epoll
- ESL作业笔记2.1.
- opencv—学习笔记6,sobel边缘检测
- Qt中数据库查询方法
- 图片大小自适应
- MATLAB匿名函数的应用1
- 内核编译问题
- LeetCode 160. Intersection of Two Linked Lists
- 中国天气网免费天气API
- Redis 集合排序
- python3.5、pyqt5项目,pyinstaller打包exe
- 源码分析HashMap初探