作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这门课:
#include <QSqlError>
#include <QMessageBox>
#include <QCompleter>
#include <QFile>
#include <QTextStream>
#include <QSqlQueryModel>
#include "items.h"
Items::Items(Ui::Store *setui)
{
form = setui;
}
void Items::getItem(int index, QString item)
{
if(index == 1)
{
sqlQuery = "SELECT * FROM Items WHERE Name='" + item + "'";
query.exec(sqlQuery);
query.next();
rec = query.record();
form->itemNumManMod->setText(query.value(rec.indexOf("ID_I")).toString());
form->purchaseManMod->setText(query.value(rec.indexOf("Purchase")).toString());
form->saleManMod->setText(query.value(rec.indexOf("Sale")).toString());
form->countityManMod->setText(query.value(rec.indexOf("Countity")).toString());
form->totaleManMod->setText(query.value(rec.indexOf("Totale")).toString());
form->minimumManMod->setText(query.value(rec.indexOf("Minimum")).toString());
form->restManMod->setText(query.value(rec.indexOf("Rest")).toString());
form->wsPriceManMod->setText(query.value(rec.indexOf("Vendor")).toString());
form->wsNumManMod->setText(query.value(rec.indexOf("Min")).toString());
QString x = idToCategory(3);
QMessageBox::critical(0, "dd", x);
}
else if(index == 2)
{
sqlQuery = "SELECT * FROM Items WHERE Name='" + item + "'";
query.exec(sqlQuery);
query.next();
rec = query.record();
form->itemNumManDel->setText(query.value(rec.indexOf("ID_I")).toString());
form->itemNameManDel->setText(query.value(rec.indexOf("Name")).toString());
form->purchaseManDel->setText(query.value(rec.indexOf("Purchase")).toString());
form->saleManDel->setText(query.value(rec.indexOf("Sale")).toString());
form->countityManDel->setText(query.value(rec.indexOf("Countity")).toString());
form->totaleManDel->setText(query.value(rec.indexOf("Totale")).toString());
form->minimumManDel->setText(query.value(rec.indexOf("Minimum")).toString());
form->restManDel->setText(query.value(rec.indexOf("Rest")).toString());
}
else if(index == 0)
{
sqlQuery = "SELECT ID_I, Sale FROM Items WHERE Name='" + item + "'";
query.exec(sqlQuery);
query.next();
rec = query.record();
form->numSaleAdd->setText(query.value(rec.indexOf("ID_I")).toString());
form->priceSaleAdd->setText(query.value(rec.indexOf("Sale")).toString());
}
}
QString Items::idToCategory(int id)
{
sqlQuery = "SELECT Name FROM Category WHERE ID_C=" + id;
query.exec(sqlQuery);
query.next();
rec = query.record();
QString name = query.value(rec.indexOf("Name")).toString();
return query.lastQuery();
}
当我调用 idToCategory()
方法时,它返回:
ECT Name FROM Items WHERE ID_C=
但是它应该返回这样的东西:
SELECT Name FROM Items WHERE ID_C=3
最佳答案
sqlQuery = "SELECT Name FROM Category WHERE ID_C=" + id;
您正在向指针添加一个整数值。试试这个:
sqlQuery = QString("SELECT Name FROM Category WHERE ID_C=%1").arg(id);
或者更好的是,使用准备好的语句,并绑定(bind)值:
sqlQuery = "SELECT Name FROM Category WHERE ID_C = :id";
query.prepare(sqlQuery);
query.bindValue(":id", id);
query.exec(); // note that you should be testing the return value of exec()
PS:对于您的其他查询,您应该查看带有QSqlTableModel
的QDataWidgetMapper
,以便一次性将所有字段值映射到每个小部件,而不是重新分配手动值。
关于c++ - 我的 SQL 查询被中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10134141/
安装并修复我的 VS2015 实例后,我仍然无法让智能感知(服务器端)在我的 MVC View 中工作。当我在 session 中第一次打开 .cshtml 文件并找到 Activitylog 文件时
我是一名优秀的程序员,十分优秀!