Solved Return boolean ( if value exist in column ) in QSqlQuery
-
@jsulm said in Return boolean ( if value exist in column ) in QSqlQuery:
SELECT * FROM 'known_users'' WHERE 'email_address' VALUES (':email')
this is wrong.
It should be:SELECT * FROM known_users WHERE email_address = ':email'
Doesn't this looks for all rows that have :email as mail address value?
Without the single quotes :email is a parameterqery.prepare("SELECT * FROM known_users WHERE email_address =:email");
-
The query should be:
qry.prepare("SELECT * FROM known_users WHERE email_address = :email ");
the part in the
TODO
isuserNameField =false; if(qry.exec()) userNameField =qry.next();
This does not check the password though, only the email
-
@Trav
Hi.bool UserLoginDlg::User_Searched(QString user, QString pass){ QSqlQuery query; query.prepare("select * from users where USERNAME ='"+user+"' and PASSWORD = '"+pass+"'"); query.exec(); while(query.next()){ return true; } return false; } void UserLoginDlg::on_Log_In_clicked() { if(User_Searched(ui->USERNAME->text(),ui->PASSWORD->text())){ User Searched :)) } else QMessageBox::information(this,"WARNING","Username Or Password Is Wrong"); }
ui->USERNAME->text(),ui->PASSWORD->text(), USERNAME AND PASSWORD Is Linedit Text
-
QSqlQuery query; query.prepare("INSERT INTO employee (id, name, salary) " "VALUES (:id, :name, :salary)"); query.bindValue(":id", 1001); query.bindValue(":name", "Thad Beaumont"); query.bindValue(":salary", 65000); query.exec();
And Read This:
http://doc.qt.io/qt-5/qsqlquery.html -
See for an easy reference on what's wrong with your code:
https://www.w3schools.com/sql/sql_injection.asp
https://www.youtube.com/watch?v=8ZtInClXe1Q -
To add
http://doc.qt.io/qt-5/qsqlquery.html#approaches-to-binding-values !!!!
ps:
why is everyone ignoring this section of the qsqlquery docs?!?!?! -
-
@Taz742 said in Return boolean ( if value exist in column ) in QSqlQuery:
This was just an example
Sorry mate but an "example" that can get you fired and/or sued it's a hell of a bad example
-
@VRonin
You are right. -
@VRonin said in Return boolean ( if value exist in column ) in QSqlQuery:
qry.prepare("SELECT * FROM known_users WHERE email_address = :email ");
Thanks, I found your reply the most helpful just from the clear response. The if statement can you please explain this a little more? the issue I guess I'm having is the if statement logic the components..
So we are declaring the userNameField = false and then entering a IF statement which is executing what exactly? and the final line "userNameField =qry.next();" we are moving to next bool? Im just a bit confused by this and want to understand this correctly so I can understand the logic thoroughly and have my hah moment , thank you. -
if(qry.exec())
just checks that the query was executed i.e. we don't have syntax errors in our query and the SQL server is respondinguserNameField =qry.next();
so if the database contains the email passed as a parameter it will return at least one row, if it does not then it will return 0 records.qry.next()
moves to the next record (the first in this case) and returns true if the record exists. What i'm doing here is asking "does the query returns at least 1 record?" to do this I try accessing the first record if it's unsuccessful it means that the query returned no rows and hence the user does not exist
P.S.
Can I ask what type is usernameField? -
@VRonin Thanks for clarifying that ! I understand and can apply this to future applications and sure... Just a QString at the moment which is entered into a UI field then passed to the function. I will take more secure practices in the future in context of the application though will defiantly want the password hashed etc. If you have any suggestions in context of something like this ( I think you understand what I'm doing here ) I'd be glad to hear them as this is my first time doing a project of this sort with communication to a Database
-
@Trav said in Return boolean ( if value exist in column ) in QSqlQuery:
Just a QString at the moment which is entered into a UI field then passed to the function.
then you need to declare another boolean instead of re-using that string
bool userNameFieldFound =false; if(qry.exec()) userNameFieldFound =qry.next();
I will take more secure practices in the future in context of the application though will defiantly want the password hashed etc. If you have any suggestions in context of something like this ( I think you understand what I'm doing here ) I'd be glad to hear them
I suggest Argon2 but QCryptographicHash with
QCryptographicHash::Sha3_512
algorithm should be ok as long as you always remember to salt it -
@VRonin Great, thanks for providing that. I was able to get my DB to check if the userNameField exist of not by using the logic you provided, I will implement what you just shared regarding the declaration as well as the security, Cheers!