Solved MySQL query
-
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
-
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.
-
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.
-
hello @SGaist and @Kent-Dorfman
I am realy sorry but i have pasted the wrong query....
the correct one iscandidate_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 -
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...
-
the question is how do i put it in C++ and in my UI form to echo the output
-
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
-
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 -
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 -
msgBox.setText(QStringLiteral("%1 PLATE IS BLACKLISTED.").arg(domain));
-
Hello m8
Thankyou verry much for all your help.
ALL is working like it should thanks to you.Until next time
Xmodpt