query value to variable?
-
wrote on 1 Jun 2020, 20:54 last edited by VRonin 6 Feb 2020, 10:12
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%"
"" -
wrote on 1 Jun 2020, 21:39 last edited by
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; -
wrote on 2 Jun 2020, 06:38 last edited by
@AliM93 said in query value to variable?:
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
Thank you very much
-
wrote on 2 Jun 2020, 09:05 last edited by
if you're ok set the post as solved! : )
-
wrote on 2 Jun 2020, 10:17 last edited by VRonin 6 Feb 2020, 10:22
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
Lifetime Qt Championwrote on 2 Jun 2020, 15:48 last edited by Christian Ehrlicher 6 Feb 2020, 15:48@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%"
""
1/7