query value to variable?
-
Hi,
probably an easy question for the experienced, but unknowable to the beginner:
My code:void MainWindow::on_pushButton_rand_clicked() { QString sk, pk, srst; sk = ui->lineEdit->text(); pk = sk.replace('\u002D', '\u0025'); // \u002D "-", \u0025 "%" QSqlQuery q(db); q.prepare("SELECT pp FROM myfirst.db WHERE pp LIKE '"+pk+"' ORDER BY RAND() LIMIT 1"); q.exec(); while (q.next()); ui->label->setText(q.value(0).toString()); srst = q.value(0).toString(); qDebug()<<sk; qDebug()<<srst; }
in
ui->label
I get the result (it is displayed), but I can't save the query result to variable. Why?For example, if I type "-ka-" in the "lineEdit" field, I get a good answer from db in "ui->label", but an answer in "Aplication Output" is :
Database open
QSqlQuery::value: not positioned on a valid record
"%ka%"
"" -
maybe you can type like this srst= ui->label->setText(.. ), in this way you can even modify you variable just typing the new one, or maye you need to include into
while (q.next());
{
ui->label->setText(q.value(0).toString());
srst = q.value(0).toString();
}
qDebug()<<sk;
qDebug()<<srst; -
This is probably due to the DB driver marking the query as forward only. It's easily solved by just doing
srst = q.value(0).toString(); ui->label->setText(srst);
pk = sk.replace('\u002D', '\u0025'); // \u002D "-", \u0025 "%"
q.prepare("SELECT pp FROM myfirst.db WHERE pp LIKE '"+pk+"' ORDER BY RAND() LIMIT 1");This is BAD. See https://www.w3schools.com/sql/sql_injection.asp
UseQSqlQuery::bindValue
or, at least,QSqlDriver::escapeIdentifier
-
This is probably due to the DB driver marking the query as forward only. It's easily solved by just doing
srst = q.value(0).toString(); ui->label->setText(srst);
pk = sk.replace('\u002D', '\u0025'); // \u002D "-", \u0025 "%"
q.prepare("SELECT pp FROM myfirst.db WHERE pp LIKE '"+pk+"' ORDER BY RAND() LIMIT 1");This is BAD. See https://www.w3schools.com/sql/sql_injection.asp
UseQSqlQuery::bindValue
or, at least,QSqlDriver::escapeIdentifier
@VRonin said in query value to variable?:
This is probably due to the DB driver marking the query as forward only.
This setting will not affect how often you can query the value from the current row and it's disabled by default so I don't think this is the reason.
-
Hi,
probably an easy question for the experienced, but unknowable to the beginner:
My code:void MainWindow::on_pushButton_rand_clicked() { QString sk, pk, srst; sk = ui->lineEdit->text(); pk = sk.replace('\u002D', '\u0025'); // \u002D "-", \u0025 "%" QSqlQuery q(db); q.prepare("SELECT pp FROM myfirst.db WHERE pp LIKE '"+pk+"' ORDER BY RAND() LIMIT 1"); q.exec(); while (q.next()); ui->label->setText(q.value(0).toString()); srst = q.value(0).toString(); qDebug()<<sk; qDebug()<<srst; }
in
ui->label
I get the result (it is displayed), but I can't save the query result to variable. Why?For example, if I type "-ka-" in the "lineEdit" field, I get a good answer from db in "ui->label", but an answer in "Aplication Output" is :
Database open
QSqlQuery::value: not positioned on a valid record
"%ka%"
""