[Solved} WHERE clause Qstring
-
Table layout example:
@[product][quantity1][ piece1][quantity2][ piece2]
[003893][ 1 ][003852][ 2 ][003845 ]@Query attempt:
@ query->prepare("SELECT quantity1 FROM prints WHERE product = ?");
query->bindValue(0, selectedPrint);@Query Result:
@QSqlQuery::value: not positioned on a valid record@
I am trying to return the number from the quantity1 column, using a variable.
My variable is the text string of my currently selected list view item:
@QString selectedPrint = ui->listView->model()->data(index).toString();@How do I use a string variable in my query?
-
Hey,
does it work without the prepare?
If not, then please show how you retrieve the records.
bb
-
Hi,
Did you run QSqlQuery::exec() and QSqlQuery::next() before QSqlQuery::value() ?
See "the documentation":http://qt-project.org/doc/qt-5/qsqlquery.html for examples.
-
Retrieval to populate product list:
@ QSqlQueryModel* modal=new QSqlQueryModel();
QSqlQuery* query=new QSqlQuery(db);query->prepare("SELECT product FROM prints"); query->exec(); modal->setQuery(*query); ui->listView->setModel(modal);@
Entire ListView that is main part of form:
@void printdb::on_listView_clicked(const QModelIndex &index)
{
QString selectedPrint = ui->listView->model()->data(index).toString();
QString direct = "prints/" % selectedPrint % ".jpg";
QPixmap g(direct);scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene); ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag); scene->addPixmap(g); QString number = QString("%1").arg(selectedPrint.toInt(), 2, 10, QChar('0')); QSqlQuery* query=new QSqlQuery(db); query->prepare("SELECT quantity1 FROM prints WHERE product = ?"); query->bindValue(0, selectedPrint); if(query->exec()) { ui->lineEdit_2->setText(query->value(0).toString()); } else qDebug() << query->lastError();@
Running without prepare statement I get
@QSqlError("", "Parameter count mismatch", "")@ -
You need to call QSqlQuery::next() before QSqlQuery::value().
By the way, you don't have to create QSqlQuery on the heap.
@
// Replace this...
QSqlQuery* query=new QSqlQuery(db);// ...with this...
QSqlQuery query(db);
@