Results 1 to 3 of 3

Thread: validation of username and password

  1. #1
    Join Date
    Oct 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default validation of username and password

    hey ,

    i wish to compare my username and password to my values put in a table say login table.

    i'm able to do this validation by using variables within program as follows:


    usr="leo";
    pass="123";

    if(!(ui->lineEdit->text()== usr && ui->lineEdit_2->text() == pass))
    {
    m.setText(" Username and Password don't match ! ! ! ! Try Again");
    m.exec();
    }
    if(ui->lineEdit->text()== usr && ui->lineEdit_2->text() == pass)
    {
    hide();
    home *homewindow = new home(this);
    homewindow->show();
    }


    this works but i want to compare with values in my database ! please help ! thanks in advance ! ! !

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: validation of username and password

    Hi,

    For this you need a database connection. So you need:

    Code in the header of the class (for example mainwindow.h)
    Qt Code:
    1. #include <QtSql>
    2. ...
    3. private:
    4.  
    5. ...
    To copy to clipboard, switch view to plain text mode 

    Code in the implementation file (cpp)
    Qt Code:
    1. db = QSqlDatabase::addDatabase("QSQLITE","dblite"); //Add a database connector to SQLite. You just need to call this once. Put it in the constructor for example
    2.  
    3. //Process for validating the user and pass using a table in sqlite
    4. db.close();
    5. db.setDatabaseName("./myDatabase.sqlite");
    6. if (!db.open()) //Try to opens the database
    7. {
    8. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    9. qApp->tr("Unable to establish a SQLite connection to the database\n\n"
    10. "Click Cancel to exit."), QMessageBox::Cancel);
    11. }
    12. else
    13. {
    14. QSqlQuery querylite(db); //Create a query using db
    15. QString sql; //SQL statement
    16. sql = "SELECT count(userName) from MyUsersTable WHERE userName = '";
    17. sql = sql + ui->lineEdit->text() + "'";
    18. sql = sql + " AND password = '";
    19. sql = sql + ui->lineEdit2->text() + "'";
    20.  
    21. if (querylite.exec(sql)) //Executed the statement
    22. {
    23. if (querylite.value(0).toInt() > 0) //If the return is bigger that 0
    24. {
    25. //User and pass was found
    26. }
    27. else
    28. {
    29. //No access
    30. }
    31. }
    32. else
    33. {
    34. //Error with the sql
    35. }
    36. }
    37. db.close();
    To copy to clipboard, switch view to plain text mode 

    Carlos

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: validation of username and password

    Hi,

    don't use QSqlDatabase as a private member, that could cause problems on quiting the application. Better use every time you need a database object the static method QSqlDatabase::database(). Use the optional parameter to identify which database connection you like.

Similar Threads

  1. Bypassing web login username with Qt
    By giusepped in forum Qt Programming
    Replies: 12
    Last Post: 19th August 2011, 00:09
  2. XML Validation
    By NoRulez in forum Qt Programming
    Replies: 4
    Last Post: 15th September 2010, 13:07
  3. Replies: 9
    Last Post: 16th November 2009, 08:26
  4. Validation
    By Tavit in forum Qt Programming
    Replies: 2
    Last Post: 21st March 2009, 07:03
  5. writing a username and password dialog
    By sergec in forum Qt Programming
    Replies: 5
    Last Post: 25th April 2007, 17:24

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
  •  
Qt is a trademark of The Qt Company.