找回密码
 立即注册
收起左侧

《开发实战精解》4-2P85中的next()作用何在?

1
回复
8599
查看
[复制链接]
累计签到:13 天
连续签到:1 天
来源: 2017-1-22 17:48:56 显示全部楼层 |阅读模式
1Qter豆
如题:
    query.next()作用是把指针前进到下一条,但是这个程序段的作用是sellBrandComboBox改变后读取产品的价格,如果指向下一条,作用何在?特别是上一条语句已经读取了改变后的产品的价格,是不是不指向下一条就不会返回上一条的数据?
我连续打入两个    query.next()编译提示错误,在该位置没有有用的数据,那么按照next的理解,它指向结果集的下一条记录,那么下一条就没有记录了,这是不是有些矛盾呢?还是说上一行代码就读取一行结果集,然后指向下一行为空?只是为了让它返回数据而已?
      query.next()它的结果是让 query返回本行的数据还是下一行的数据呢?
代码如下:

// 出售商品的品牌改变时
void Widget:n_sellBrandComboBox_currentIndexChanged(QString brand)
{
    ui->sellNumSpinBox->setValue(0);
    ui->sellNumSpinBox->setEnabled(false);
    ui->sellSumLineEdit->clear();
    ui->sellSumLineEdit->setEnabled(false);
    ui->sellOkBtn->setEnabled(false);

    QSqlQuery query;
    query.exec(QString("select price from brand where name='%1' and type='%2'")
               .arg(brand).arg(ui->sellTypeComboBox->currentText()));
    query.next();  //这里
    ui->sellPriceLineEdit->setEnabled(true);
    ui->sellPriceLineEdit->setReadOnly(true);
    ui->sellPriceLineEdit->setText(query.value(0).toString());

    query.exec(QString("select last from brand where name='%1' and type='%2'")
               .arg(brand).arg(ui->sellTypeComboBox->currentText()));
    query.next();
    int num = query.value(0).toInt();

    if (num == 0) {
        QMessageBox::information(this, tr("提示"), tr("该商品已经售完!"), QMessageBox::Ok);
    } else {
        ui->sellNumSpinBox->setEnabled(true);
        ui->sellNumSpinBox->setMaximum(num);
        ui->sellLastNumLabel->setText(tr("剩余数量:%1").arg(num));
        ui->sellLastNumLabel->setVisible(true);
    }
}

回复

使用道具 举报

累计签到:13 天
连续签到:1 天
2017-1-23 16:52:51 显示全部楼层
已经知道了。是我自己把自己绕晕了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了