QSqlQuery select statment
-
hallo all
i will let the code talkQSqlQuery query; query.prepare("SELECT * FROM [main].[items] WHERE barcode = ? OR name = ? "); query.bindValue(0,ui->Numberinput->text()); query.bindValue(1,ui->search_item->text()); query.exec(); query.first(); ui->Cart->setItem(ui->Cart->currentRow(), ui->Cart->currentColumn(), new QTableWidgetItem(query.value(1).toString()));
the error i get
QSqlQuery::value: not positioned on a valid record
-
hallo all
i will let the code talkQSqlQuery query; query.prepare("SELECT * FROM [main].[items] WHERE barcode = ? OR name = ? "); query.bindValue(0,ui->Numberinput->text()); query.bindValue(1,ui->search_item->text()); query.exec(); query.first(); ui->Cart->setItem(ui->Cart->currentRow(), ui->Cart->currentColumn(), new QTableWidgetItem(query.value(1).toString()));
the error i get
QSqlQuery::value: not positioned on a valid record
@MrLibya
are you sure that the query returns a valid result (QSqlQuery::isValid()
)?
Maybe you could iterate over the result and check it. -
@MrLibya
are you sure that the query returns a valid result (QSqlQuery::isValid()
)?
Maybe you could iterate over the result and check it.@raven-worx yes query.isValid() return false , will we don't really need that ! , i'm already know the query is not Valid , but how to write the correct syntax for this query
-
@raven-worx yes query.isValid() return false , will we don't really need that ! , i'm already know the query is not Valid , but how to write the correct syntax for this query
qDebug() << query.lastError().text()
should give more hints whats the problem.
But i guess the problem is the table name (
[main].[items]
), which isn't valid?!? -
qDebug() << query.lastError().text()
should give more hints whats the problem.
But i guess the problem is the table name (
[main].[items]
), which isn't valid?!?@raven-worx ok i got
Parameter count mismatch
-
i also tried
query.addBindValue(ui->Numberinput->text()); query.addBindValue(ui->search_item->text());
but the same
@MrLibya
this error is very unspecific and may also occur when the query itself is invalid.
As i already stated, whats about the table name[main].[items]
. Is this really correct?! -
@MrLibya
this error is very unspecific and may also occur when the query itself is invalid.
As i already stated, whats about the table name[main].[items]
. Is this really correct?!@raven-worx yep , i've already used this method in another function and it's work very well
void MainWindow::UpdateSearchItem(QStringList *List) { QSqlQuery query("SELECT * FROM [main].[items]"); while (query.next()) *List << query.value(1).toString(); }
-
Hi, maybe the text substitution goes haywire, to see the resolved text, try:
qDebug() << query.executedQuery();
@hskoglund said in QSqlQuery select statment:
qDebug() << query.executedQuery();
i got
"SELECT * FROM [main].[items] WHERE barcode = ? OR name = ? "
-
@hskoglund said in QSqlQuery select statment:
qDebug() << query.executedQuery();
i got
"SELECT * FROM [main].[items] WHERE barcode = ? OR name = ? "
-
Indeed the text substitution seems borked, you could try instead doing the text substitution directly yourself, something like this:
QSqlQuery query("SELECT * FROM [main].[items] WHERE barcode = '123' OR name = 'Smith'");
@hskoglund that will not really helps , goes the barcode - name , the user will input from line edit , so dose this only not work on sqlite ? if i use mysql it will work ??
-
Hi, you can build the select string something like this:
QString s = QString("SELECT * FROM [main].[items] WHERE barcode = '%1' OR name = '%2'").arg(ui->Numberinput->text()).arg(ui->search_item->text()); QSqlQuery query(s);
@hskoglund thx it work :)