PDA

View Full Version : So how to write the part of login to the database with the sql command .



wter27
1st February 2011, 13:02
QString var_s_user,var_s_pass,var_s_sql;
var_s_user=ui->lineEdit_user->text().trimmed();
var_s_pass=ui->lineEdit_pass->text().trimmed();
QSqlQuery query_cmd;
var_s_sql="select username from user where username = " +var_s_user + " and password="+var_s_pass+";";
query_cmd.exec(var_s_sql);
I have finished the connection to the database .And it could connect to the data normally.
But I just cant solve the login part.So how to check if the username and password is right.
I use the code QSqlRecord rec = query_cmd.record();to find out if there is a right username and password in the database.But it did not work .So anyone could give me some examples?

mcosta
4th February 2011, 11:00
When you use SELECT you must use QSqlQuery::next() to retreive records



QSqlQuery query_cmd("select username from user where username = "
+ var_s_user + " and password="+var_s_pass);
if (query_cmd.next()) {
// Data found
}


IN this case you can use QSqlQuery::first() instead of QSqlQuery::next() because you are intersted only on the presence of one record

wter27
5th February 2011, 02:11
thankyou very much.If there is only the way you just mentioned .I am glad to hear that.

kornicameister
5th February 2011, 11:40
I've got a question ?
Why do you need checking the correction of username and pass ?
I am just asking, because this thought is literally piercing through my brain :D and I can stop myself from asking :)