Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. query value to variable?

query value to variable?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 627 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eidus
    wrote on last edited by VRonin
    #1

    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%"
    ""

    VRoninV 1 Reply Last reply
    0
    • A Offline
      A Offline
      AliM93
      wrote on last edited by
      #2

      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;

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Eidus
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        1
        • A Offline
          A Offline
          AliM93
          wrote on last edited by
          #4

          if you're ok set the post as solved! : )

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            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
            Use QSqlQuery::bindValue or, at least, QSqlDriver::escapeIdentifier

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            Christian EhrlicherC 1 Reply Last reply
            1
            • VRoninV VRonin

              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
              Use QSqlQuery::bindValue or, at least, QSqlDriver::escapeIdentifier

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #6

              @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.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • E Eidus

                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%"
                ""

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                @Eidus Now I'm curious to find out why then, what DB driver (sqlite, mysql, etc) are you using?

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved