Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. MySQL query
QtWS25 Last Chance

MySQL query

Scheduled Pinned Locked Moved Solved C++ Gurus
11 Posts 4 Posters 1.5k 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.
  • XmodptX Offline
    XmodptX Offline
    Xmodpt
    wrote on last edited by
    #1

    Hello everyone

    I am new in C++ and like a "noob" i have a lot of dummmmm questions and one of those is:

    Scenario:

    i have a ocr app running and when it detects a set of char, it prints it on-screen in to a label and wright's it into to a MySql database.

    Question:

    Is there any way or "easy way" to read the database to check if the text on label exists and if it does, open a UI popup and echo the search value and a Warning message ?

    My Insert query

    candidate_query := INSERT INTO candidates (domain, matches, confidence, plate_detection_id) VALUES (:d, :m, :c, :pdid);
    

    My Select query

    candidate_query := SELECT * FROM candidates WHERE domain = :d;
    

    Code where the mojo happens (main CPP)

    int Database::candidate(QString domain, QString matches, float confidence, int plateDetectionId)
    {
    
        QSqlQuery query;
        query.prepare(getCandidateQuery());
        query.bindValue(":d", domain);
        query.bindValue(":m", matches);
        query.bindValue(":c", confidence);
        query.bindValue(":pdid", plateDetectionId);
    
        if(query.exec() == false)
        {
            LOG_ERR(QString("Database: " + query.lastError().text()));
            return -1;
        }
    
        return query.lastInsertId().toInt();
    }
    

    Can anyone help a noob

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      A select query that returns nothing is likely what you want to check.

      What exactly is your issue with that ?

      One tip, if you only want to check that it exists, no need to query the content of the whole table, only the field you are interested in is enough.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        In addition to what @SGaist suggested you can make the database table use a unique key on the data fields being inserted. That way if you try to insert the same data more than once the transaction will fail. At least that is the strategy I do in PostgreSQL. I assume unique keys are supported in mySQL.

        1 Reply Last reply
        0
        • XmodptX Offline
          XmodptX Offline
          Xmodpt
          wrote on last edited by Xmodpt
          #4

          hello @SGaist and @Kent-Dorfman

          I am realy sorry but i have pasted the wrong query....
          the correct one is

          candidate_query := SELECT domain FROM candidates WHERE domain = :d;
          

          It seems that i haven't explain my self correctly

          the intent is not to "input" data into the table but read from data from it.

          in a nut shell....

          Camera reads plate -> check if plate exists in DB -> if exists... shows a Warning Message that includes the detected plate + custom text... if not... keep reading.

          NOTE: the data in the database is a user input process via a dashboard manualy.

          Hope'd i was able to clarify my questions....

          regards
          NP

          1 Reply Last reply
          0
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5

            then simply write a query looking for that license number. If the number of rows returned equals zero then it was not found. pretty simple...

            1 Reply Last reply
            0
            • XmodptX Offline
              XmodptX Offline
              Xmodpt
              wrote on last edited by
              #6

              the question is how do i put it in C++ and in my UI form to echo the output

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7
                QSqlQuery query;
                query.prepare("SELECT domain FROM candidates WHERE domain = :d");
                query.bindValue(":d", domain);
                if(!query.exec())
                qDebug() << "Unable to execute query";
                if(query.next()){
                qDebug() << "The domain is already in the database";
                }
                

                shows a Warning Message

                https://doc.qt.io/qt-5/qmessagebox.html#warning

                "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

                XmodptX 1 Reply Last reply
                2
                • XmodptX Offline
                  XmodptX Offline
                  Xmodpt
                  wrote on last edited by Xmodpt
                  #8

                  @VRonin

                  Thank you for your help. I will test it tomorrow and I'll let you know.

                  Ps- in order the get the warning to work on my UI all I need is to change from qdebug to ui->

                  Thank you
                  Np

                  1 Reply Last reply
                  0
                  • VRoninV VRonin
                    QSqlQuery query;
                    query.prepare("SELECT domain FROM candidates WHERE domain = :d");
                    query.bindValue(":d", domain);
                    if(!query.exec())
                    qDebug() << "Unable to execute query";
                    if(query.next()){
                    qDebug() << "The domain is already in the database";
                    }
                    

                    shows a Warning Message

                    https://doc.qt.io/qt-5/qmessagebox.html#warning

                    XmodptX Offline
                    XmodptX Offline
                    Xmodpt
                    wrote on last edited by Xmodpt
                    #9

                    @VRonin

                    Hello m8

                    It worked like a charm... thank you very much for the tip.

                    Let me ask you guys a tip:

                    I have made some changes but what i need to get the a custom output in the QMessageBox. something like

                    "Warning: XX@@ZZ (domain VAR) plate is blacklisted!"

                    is there a way to output my (domain) var in the QMessageBox ?

                    My current code:

                    
                        QSqlQuery query;
                        query.prepare("SELECT domain FROM candidates WHERE domain = :d");
                        query.bindValue(":d", domain);
                        if(!query.exec())
                        qDebug() << "Unable to execute query";
                        if(query.next()){
                        qDebug() << "The domain is already in the database";
                            QMessageBox msgBox;
                            QPixmap myIcon(":/images/danger.png");
                            msgBox.setIconPixmap(myIcon);
                            msgBox.setText("PLATE IS BLACKLISTED.");
                            msgBox.exec();
                    

                    Also i have tried using a UI Form but i was unable to print my "domain" value output

                    //////// UI FORM - msgbox.ui///////

                          MsgBox msgbox;
                           msgbox.setModal(true);
                           msgbox.exec();
                    

                    ///////////////////////////////////////////////////////////////////////////////////

                    Kind Regards
                    Xmodpt

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

                      msgBox.setText(QStringLiteral("%1 PLATE IS BLACKLISTED.").arg(domain));

                      "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

                      XmodptX 1 Reply Last reply
                      1
                      • VRoninV VRonin

                        msgBox.setText(QStringLiteral("%1 PLATE IS BLACKLISTED.").arg(domain));

                        XmodptX Offline
                        XmodptX Offline
                        Xmodpt
                        wrote on last edited by
                        #11

                        @VRonin

                        Hello m8

                        Thankyou verry much for all your help.
                        ALL is working like it should thanks to you.

                        Until next time
                        Xmodpt

                        1 Reply Last reply
                        1

                        • Login

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