Results 1 to 5 of 5

Thread: How to check Primary key is Exists in Table ?

  1. #1
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to check Primary key is Exists in Table ?

    I have a Database with Table SinhVien ( MSSV, HoTen, Lop_MaLop ),

    1.jpg

    I created QT C++ project with Buttons: Add, Edit, Del and connect to my Database (QMySQL). When I enter a value to MSSV and press Edit Button, will be edit information of HoTen, Lop_MaLop look like code :

    2.jpg

    QSqlQuery *qry=new QSqlQuery();
    qry->prepare(
    "UPDATE SinhVien SET HoTen = '" + ui.txtTenSinhVien->text()
    + "', Lop_MaLop = " + ui.txtLop->text() + " where MSSV = "
    + ui.txtMaSV->text() + "");
    qry->exec();

    It's work but we can not know the value entered on MSSV is Exists or Not Exists in Database (Table MSSV ), How to check ? I try this code but not work :

    QSqlQuery *qry=new QSqlQuery();

    if("exists(select MSSV from SinhVien where MSSV = )" + ui.txtMaSV->text())
    {
    qry->prepare(
    "UPDATE SinhVien SET HoTen = '" + ui.txtTenSinhVien->text()
    + "', Lop_MaLop = " + ui.txtLop->text() + " where MSSV = "
    + ui.txtMaSV->text() + "");
    qry->exec();
    QMessageBox::information(this, QString::fromUtf8("Yeah !."),
    QString::fromUtf8("Edit successful !"));
    ShowData();
    }
    else
    {
    //QMessageBox::information(this, QString::fromUtf8("Warning !."),
    //QString::fromUtf8("MSSV Not Exists in Database!"));
    return;
    }


    Thanks for Help !

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

    thaihoangluu (28th December 2011)

  3. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to check Primary key is Exists in Table ?

    You need to build an SQL query with that exists clause in the where part of the query. execute it, and look at the result.

    You should also avoid building queries by joining query fragments as strings. This is hard to read and maintain, is easy to break, and opens up security issues. Use named parameters and the bindValue() function. See Approaches to Binding Values
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  4. The following 2 users say thank you to ChrisW67 for this useful post:

    hohoanganh205 (25th December 2011), thaihoangluu (28th December 2011)

  5. #3
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to check Primary key is Exists in Table ?

    Thanks ChrisW67,
    I read bindValue() function and will be apply to code.

    see you again

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

    thaihoangluu (28th December 2011)

  7. #4
    Join Date
    Dec 2011
    Posts
    14
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to check Primary key is Exists in Table ?

    I finished my code, share to another Beginners:

    Qt Code:
    1. QSqlQuery *checkexists=new QSqlQuery();
    2. checkexists->prepare("select MaDaiLy from DaiLy where MaDaiLy = " + ui.txtMaDL->text());
    3. checkexists->exec();
    4. if((checkexists->last())==true)
    5. {
    6. //exists
    7. }
    8. else
    9. {
    10. //not exists
    11. // do something.
    12. }
    To copy to clipboard, switch view to plain text mode 

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

    thaihoangluu (28th December 2011)

  9. #5
    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: How to check Primary key is Exists in Table ?

    This is not working because You are executing query like this :
    Qt Code:
    1. select MaDaiLy from DaiLy where MaDaiLy = blablabla
    To copy to clipboard, switch view to plain text mode 
    and of course You got an error from SQL engine because You don't have column 'blablabla' in table DaiLy. You should do this like :
    Qt Code:
    1. checkexists->prepare("select MaDaiLy from DaiLy where MaDaiLy = ?");
    2. checkexists->bindValue(0, ui.txtMaDL->text());
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 25th June 2012, 10:54
  2. Replies: 2
    Last Post: 21st February 2011, 14:52
  3. Check if item exists into listwidget
    By hakermania in forum Qt Programming
    Replies: 1
    Last Post: 26th January 2011, 21:54
  4. Check if a table exists in a mysql database
    By graciano in forum Qt Programming
    Replies: 8
    Last Post: 5th November 2009, 02:44
  5. Check if window with certain title exists
    By devil in forum Qt Programming
    Replies: 5
    Last Post: 21st January 2009, 14:42

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.