QSqlQuery不显示QString位置
我犯了这样的错误QSqlQuery不显示QString位置,sql,qt,Sql,Qt,我犯了这样的错误 QSqlQuery::value: not positioned on a valid record 以下是代码本身: void MainWindow::on_pushButton_clicked() { QString inputText = ui->lineEdit->text(); qDebug() << "inputText :" << inputText; double zapros = inputText
QSqlQuery::value: not positioned on a valid record
以下是代码本身:
void MainWindow::on_pushButton_clicked()
{
QString inputText = ui->lineEdit->text();
qDebug() << "inputText :" << inputText;
double zapros = inputText.toDouble();
// считываем данные из базы
QSqlQuery query;
if(!query.exec("SELECT * FROM " TABLE ";")){
qDebug() << "Unable to execute query - exiting";
}
QSqlRecord rec = query.record();
double BarCode;
QString Name;
QDate Date;
bool tex_per=false;
while(query.next()){
BarCode = query.value(rec.indexOf("BarCode")).toDouble();
qDebug() << "BarCode :" <<BarCode;
if(BarCode == zapros){
Name = query.value(rec.indexOf("Message")).toString();
Date = query.value(rec.indexOf("Date")).toDate();
qDebug()<<"|"<<BarCode<<"|"<<Name<<"|"<<Date<<"|";
tex_per = true;
}
}
if(tex_per==false){
qDebug()<<"Такой штрих-код в базе данных не имеется";
}
}
(程序)获取流动数据,但同时由于某种原因,没有给出一个名为“name”的位置,该位置位于QString格式中。这正是问题所在,程序给出的QSqlQuery::value:未定位在有效记录上
在数据库中,使用此行将VARCHAR(255)
插入数据库
query.bindValue(":Message", data[1].toString());
我们需要检查数据库中是否有这样一个“Message”字段 添加代码
QSqlRecord rec = query.record();
//- news
for(int i=0;i<rec.count();i++) { qDebug()<<"rec.fieldName("<<i<<"):"<<rec.fieldName(i); }
//-
double BarCode;
QString Name;
QDate Date;
没有“消息”位置,但有“名称”
所有添加的代码
void MainWindow::on_pushButton_clicked()
{
QString inputText = ui->lineEdit->text();
qDebug() << "inputText :" << inputText;
double zapros = inputText.toDouble();
// считываем данные из базы
QSqlQuery query;
if(!query.exec("SELECT * FROM " TABLE ";")){
qDebug() << "Unable to execute query - exiting";
}
QSqlRecord rec = query.record();
for(int i=0;i<rec.count();i++) { qDebug()<<"rec.fieldName("<<i<<"):"<<rec.fieldName(i); }
double BarCode;
QString Name;
QDate Date;
bool tex_per=false;
while(query.next()){
BarCode = query.value(rec.indexOf("BarCode")).toDouble();
qDebug() << "BarCode :" <<BarCode;
if(BarCode == zapros){
Name = query.value(rec.indexOf("Name")).toString(); // to String()
Date = query.value(rec.indexOf("Date")).toDate();
qDebug()<<"|"<<BarCode<<"|"<<Name<<"|"<<Date<<"|";
tex_per = true;
}
}
if(tex_per==false){
qDebug()<<"Такой штрих-код в базе данных не имеется";
}
}
inputText : "4690432003609"
rec.fieldName( 0 ): "id"
rec.fieldName( 1 ): "BarCode"
rec.fieldName( 2 ): "Name"
rec.fieldName( 3 ): "Date"
void MainWindow::on_pushButton_clicked()
{
QString inputText = ui->lineEdit->text();
qDebug() << "inputText :" << inputText;
double zapros = inputText.toDouble();
// считываем данные из базы
QSqlQuery query;
if(!query.exec("SELECT * FROM " TABLE ";")){
qDebug() << "Unable to execute query - exiting";
}
QSqlRecord rec = query.record();
for(int i=0;i<rec.count();i++) { qDebug()<<"rec.fieldName("<<i<<"):"<<rec.fieldName(i); }
double BarCode;
QString Name;
QDate Date;
bool tex_per=false;
while(query.next()){
BarCode = query.value(rec.indexOf("BarCode")).toDouble();
qDebug() << "BarCode :" <<BarCode;
if(BarCode == zapros){
Name = query.value(rec.indexOf("Name")).toString(); // to String()
Date = query.value(rec.indexOf("Date")).toDate();
qDebug()<<"|"<<BarCode<<"|"<<Name<<"|"<<Date<<"|";
tex_per = true;
}
}
if(tex_per==false){
qDebug()<<"Такой штрих-код в базе данных не имеется";
}
}
inputText : "4690432003609"
rec.fieldName( 0 ): "id"
rec.fieldName( 1 ): "BarCode"
rec.fieldName( 2 ): "Name"
rec.fieldName( 3 ): "Date"
BarCode : 9.0238e+12
BarCode : 4.69043e+12
| 4.69043e+12 | "Корректирующая жидкость" | QDate("2017-08-12") |