hohoanganh205
23rd December 2011, 05:19
I have a Database with Table SinhVien ( MSSV, HoTen, Lop_MaLop ),
7199
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 :
7200
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 !
7199
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 :
7200
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 !