Results 1 to 7 of 7

Thread: Check credentials using QSql

  1. #1
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Check credentials using QSql

    i wrote the following function to make a simple login check.username and password are two global QString variables(values entered by user via a line edit). but this function always returns false for some reason. the commented out query.exec works fine(returns true when user present,false otherwise). i have a feeling its something to do with my query.prepare, but not able to lay my finger on it.any ideas?

    bool check_cred()
    {
    QSqlQuery query;
    query.prepare("select * from credentials where uname=':id' and passwd=':pd'");
    query.bindValue(":id", username);
    query.bindValue(":pd", password);
    query.exec();
    //query.exec("select * from credentials where uname='ru' and passwd='ru'");
    if(query.size()==0)
    return false;
    else
    return true;
    }

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Check credentials using QSql

    Check the values of 'username' and 'password', maybe they are empty strings.
    It wont hurt to check this too (after exec()):
    QSqlQuery::lastError()

  3. #3
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Check credentials using QSql

    hi,
    ensure tat
    Qt Code:
    1. select * from credentials where uname='ru' and passwd='ru'
    To copy to clipboard, switch view to plain text mode 
    gives any result in database. run the above query in the database and check.
    and run the qDebug()<<query.executedQuery(); in db.

    Bala

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Check credentials using QSql

    Get rid of the quotation marks from your query. Also look at my modifications of your query.
    Qt Code:
    1. bool check_cred()
    2. {
    3. QSqlQuery query;
    4. query.prepare("select COUNT(*) from credentials where uname=:id and passwd=:pd");
    5. query.bindValue(":id", username);
    6. query.bindValue(":pd", password);
    7. if(!query.exec() || !query.next()) return false;
    8. return (query.value(0).toInt()>0);
    9. }
    To copy to clipboard, switch view to plain text mode 
    Better yet don't store plaintext passwords but rather some hashes (like sha1 or md5).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Check credentials using QSql

    no they are not empty strings.checked that.

    @bala the query returns a result in the database, no problem.the commented exec() works fine

    @wysota tried the modified code but still the same problem. always returns false.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Check credentials using QSql

    This might sound stupid but did you open the database before running the query? Show us the code please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    death_star (26th January 2011)

  8. #7
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Check credentials using QSql

    Whoa!! you were right wysota! there was something wrong in the code i was using for setting up the connection. should have eliminated all the common problems before running here... Thanks for all the help! used db.setHostName("127.0.0.1"); instead of db.setHostName("localhost");. but still doesnt explain why the commented query worked though.

Similar Threads

  1. How to save credentials in local keychain
    By kalos80 in forum Qt Programming
    Replies: 4
    Last Post: 9th November 2010, 11:08
  2. Using auto_increment in QSql
    By SykeS in forum Newbie
    Replies: 3
    Last Post: 21st May 2010, 07:13
  3. QSql Database
    By addu in forum Qt Programming
    Replies: 6
    Last Post: 20th July 2009, 13:37
  4. QSql error
    By renjithmamman in forum Qt Programming
    Replies: 4
    Last Post: 20th December 2006, 10:28
  5. Cannot use QSql drivers
    By xgoan in forum Newbie
    Replies: 2
    Last Post: 25th August 2006, 15:45

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.