Results 1 to 5 of 5

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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)

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