|
1Qter豆
我做的一个物流管理系统,也就是连接数据库能够实现对数据库的增删改查,为了增加功能,我还做了一个登录窗口。但是我用query.exec(sql),sql是提前准备好的QString字符串,但是能够select查询Mysql数据库,不能够update更新数据库。
关键代码如下
- void MainWindow::on_Button_sure_clicked()
- {
- //获取信息
- //(1)销售数量
- int num = ui->spinBox->value();
- //(2)剩余
- int last = ui->label_last->text().toInt();
- //获取数据库的销量
- QSqlDatabase::database().transaction();
- QSqlQuery query;
- QString sql = QString("select sell from brand where factory = '%1' and name = '%2'")
- .arg( ui->comboBox_factory->currentText() )
- .arg( ui->comboBox_brand->currentText() );
- //执行数据库
- query.exec(sql);
- int sell;
- while(query.next())
- {
- sell = query.value("sell").toInt();
- }
- //更新数据库,剩余数量,销售总量
- sell += num;
- sql = QString("update brand set sell=%1,last=%2 where factory = '%3' and name = '%4'")
- .arg(sell)
- .arg(last)
- .arg( ui->comboBox_factory->currentText() )
- .arg( ui->comboBox_brand->currentText() );
- //执行数据库
- query.exec(sql);
- }
复制代码 执行查询语句的query.exec()能够准确执行,但是执行update更新数据库的query.exec()执行结束后,我刷新数据库并没有反应
|
|