Results 1 to 8 of 8

Thread: [Qt/Sql] SQL Authentification : what query execute ?

  1. #1
    Join Date
    May 2013
    Posts
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question [Qt/Sql] SQL Authentification : what query execute ?

    Hi!

    First of all, you need to now that I'm not very fluent in English, then be lenient if some of my explanation are not very good.

    I'm trying to create some sort of authentication software on my stage, we give a pair username / pwd then the software must check whether the username exists in the database then check if pwd matches.
    But I stumble on two problems :

    - The first : the pwd stored in the SQL database are hashed (and maybe salty), and I wonder if with Qt, it could be hashed and salted in the same way so that both pwd correspond to history compare, if so, how?
    - The second : I don't know what to request and what order to perform the above steps, I would like to know if you could also help me on this point (possibly with specific code).

    Here is my actual code ( these are just attempts ):

    Qt Code:
    1. void loginFen::buttonConnect_onClicked()
    2. {
    3. QSqlQuery query;
    4. query.prepare("SELECT mdp from users WHERE pseudo=:pseudo");
    5. query.bindValue(":pseudo", pseudo->text());
    6.  
    7. if(query.exec())
    8. {
    9. //query.first();
    10. if(query.value(2).toString() == password->text()) // the problem is here
    11. {
    12. close();
    13. Fenetre *Fen = new Fenetre;
    14. Fen->show();
    15. Fen->loadStyle();
    16. }
    17. else
    18. {
    19. QMessageBox::information(this, "Erreur de saisie", "Le mot de passe que vous avez entré est incorrect, veuillez réessayer"); // wrong password
    20. }
    21. }
    22. else
    23. {
    24. QMessageBox::information(this, "Erreur de saisie", "Le pseudo que vous avez entré est incorrect, veuillez réessayer"); wrong pseudo
    25. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your help anyway & thank you to tell me if it is not accurate enough, in which case, I'll explain again my problem !

  2. The following user says thank you to CR4SH for this useful post:


  3. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt/Sql] SQL Authentification : what query execute ?

    First of all in line 4 You are taking from database only one value (mdp) but in line 10 You are trying to read value number 2 from result. Lines 9 and 10 should looks like :
    Qt Code:
    1. if( query.first() )//position on first record. if false then SELECT returns nothing
    2. if(query.value(0).toString() == password->text())
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to Lesiok for this useful post:


  5. #3
    Join Date
    May 2013
    Posts
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Qt/Sql] SQL Authentification : what query execute ?

    Thanks you for your help, now, if pseudo AND pwd is correct, then it work. But, how to do for check for existing pseudo/ pwd in database, like any kind of authentication system, which query I need ?
    I've read that, in PHP, we need to use something like :

    Qt Code:
    1. <?php
    2. $req = $bdd->prepare("SELECT COUNT(*) FROM profil WHERE pseudo = :pseudo");
    3. $req->bindValue(':pseudo', $_SESSION['pseudo'], PDO::PARAM_STR);
    4. $req->execute();
    5. $nb = $req->fetchColumn();
    6. if($nb == 0)
    7. echo 'Pseudo not found';
    8. else
    9. // ...
    To copy to clipboard, switch view to plain text mode 

    But i don't know how to do in Qt.

  6. The following user says thank you to CR4SH for this useful post:


  7. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt/Sql] SQL Authentification : what query execute ?

    Exactly the same as in PHP. After all, your main tool is here SQL and not PHP or C++.

  8. The following user says thank you to Lesiok for this useful post:


  9. #5
    Join Date
    May 2013
    Posts
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Qt/Sql] SQL Authentification : what query execute ?

    Yes but which function works the same as fetchColumn ? And what will be the type of $nb in case of Qt use ?

  10. The following user says thank you to CR4SH for this useful post:


  11. #6
    Join Date
    May 2013
    Posts
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Qt/Sql] SQL Authentification : what query execute ?

    Does anyone can help me ? I really need help to do this ...

  12. The following user says thank you to CR4SH for this useful post:


  13. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt/Sql] SQL Authentification : what query execute ?

    Think a little. In the first email you know how to read a value from the database and now not ?
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("SELECT COUNT(*) FROM profil WHERE pseudo = :pseudo");
    3. query.bindValue(":pseudo", pseudo->text());
    4. if(query.exec())
    5. {
    6. query.first();
    7. if( query.value(0).toInt() == 0 )
    8. {//not found
    9. }
    10. else
    11. {//found
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

  14. The following user says thank you to Lesiok for this useful post:


  15. #8
    Join Date
    May 2013
    Posts
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [Qt/Sql] SQL Authentification : what query execute ?

    Thanks for your help, i finaly found how to do.

  16. The following user says thank you to CR4SH for this useful post:


Similar Threads

  1. Lua + C++ Function Execute
    By steadi in forum Newbie
    Replies: 1
    Last Post: 2nd December 2012, 17:44
  2. QSqlQuery does not execute
    By weixj2003ld in forum Qt Programming
    Replies: 7
    Last Post: 11th July 2012, 12:13
  3. Execute a .sql file with more than one query
    By ShadowBelmolve in forum Newbie
    Replies: 6
    Last Post: 19th August 2010, 16:48
  4. Can execute examples
    By gyre in forum Qwt
    Replies: 12
    Last Post: 26th February 2010, 08:51
  5. Replies: 2
    Last Post: 5th March 2008, 10:39

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.