zoukankan      html  css  js  c++  java
  • Qt QSqlquery结果集使用——size()函数无法返回结果集个数

    QSqlquery结果集使用记录

    知识点:

    query = new QSqlquery(db);  
    或者  
    query = QSqlquery::QSqlquery(db);
    
    执行query.exec(str);成功后,query就包含了结果集,可以直接操作query指针,来读取QSqlResult结果集中的内容

    Query常用函数:

    seek(int n) :query指向结果集的第n条记录;
    first() :query指向结果集的第一条记录;
    last() :query指向结果集的最后一条记录;
    next() :query指向下一条记录,每执行一次该函数,便指向相邻的下一条记录;
    previous() :query指向上一条记录,每执行一次该函数,便指向相邻的上一条记录;
    record() :获得现在指向的记录;
    value(int n) :获得属性的值。其中n表示你查询的第n个属性,比方上面我们使用“select * from student”就相当于“select id, name from student”,那么value(0)返回id属性的值,value(1)返回name属性的值。该函数返回QVariant类型的数据,关于该类型与其他类型的对应关系,可以在帮助中查看QVariant。
    at() :获得现在query指向的记录在结果集中的编号。

    Size()函数无法返回结果集个数,返回-1

    原因是函数本身问题,Qt帮助索引中描述如下:

    int QSqlQuery::size () const
    
    Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes. Note that for non-SELECT statements (isSelect() returns false), size() will return -1. If the query is not active (isActive() returns false), -1 is returned.
    
    To determine the number of rows affected by a non-SELECT statement, use numRowsAffected().
    
    See also isActive(), numRowsAffected(), and QSqlDriver::hasFeature().

    翻译:

      返回结果的大小(返回的行数),如果无法确定大小或数据库不支持有关查询大小的报告信息,则返回-1。注意,对于非select语句(isselect()返回false),size()将返回-1。如果查询未处于活动状态(is active()返回false),则返回-1。

     要确定受非select语句影响的行数,请使用numrowsAffected()。

    解决方法:

    //----------获取结果集大小----------//
    
            if (query.last())
    
            {
    
                qDebug()<<"结果集大小="<<query.at() + 1;
    
            }
    
            query.first();//重新定位指针到结果集首位
    
            query.previous();//如果执行query.next来获取结果集中数据,要将指针移动到首位的上一位。
  • 相关阅读:
    LeetCode 538----Convert BST to Greater Tree
    LeetCode 543---- Diameter of Binary Tree
    springboot请求体中的流只能读取一次的问题
    bind9+dlz+mysql连接断开问题
    关于Java的Object.clone()方法与对象的深浅拷贝
    HashSet怎样保证元素不重复
    percona-toolkit主从同步整理(MySQL)
    MySQL主从配置
    关于自动化部署平台的尝试
    struts2使用注解的时候遇到的问题
  • 原文地址:https://www.cnblogs.com/shuoguoleilei/p/11425397.html
Copyright © 2011-2022 走看看