• 4781阅读
  • 1回复

[讨论]不符合外键就直接退出也太狠了吧。。 [复制链接]

上一主题 下一主题
离线sjyzhxw
 
只看楼主 倒序阅读 楼主  发表于: 2012-05-24
  1. void AdministratorMainWindow::insertCourse()
  2. {
  3.     bool successful=false;
  4.     QMessageBox *errorMsg=0;
  5.     if(courseIDLine->text ().isEmpty () || courseNameLine->text().isEmpty ()
  6.             ||courseTeacherIDLine->text ().isEmpty ())
  7.     {
  8.         errorMsg=new QMessageBox(QMessageBox::Information,tr("添加失败!"),
  9.                                               tr("请填满信息"),QMessageBox::Ok,this);
  10.         errorMsg->show();
  11.     }
  12.     else {
  13.         
  14.         //不符合外键会直接退出!
  15.         query->prepare ("insert into Courses values (?,?,?)");
  16.         query->bindValue (0,courseIDLine->text ());
  17.         query->bindValue (1,courseNameLine->text ());
  18.         query->bindValue (2,courseTeacherIDLine->text ());
  19.         successful=query->exec ();
  20.     }if(successful)
  21.     {
  22.         closeInsertCourseDlg ();
  23.     }else if(!errorMsg->isVisible ())
  24.     {
  25.         errorMsg=new QMessageBox(QMessageBox::Information,tr("添加失败!"),
  26.                                               tr("无法添加课程信息,可能是因为所填教师不存在!"),QMessageBox::Ok,this);
  27.         errorMsg->show();
  28.     }
  29. }
本来指望如果出错successful为false还能判断一下,结果直接来个

提示:

[size=; font-size:12pt,12pt][font='Consolas']QODBCResult::exec: Unable to execute statement: "[Microsoft][ODBC SQL Server Driver][SQL Server]INSERT 语句与 FOREIGN KEY 约束"FK_TeacherID_Course"冲突。该冲突发生于数据库"SchoolManagement",表"dbo.Teachers", column 'teacherID'。 [Microsoft][ODBC SQL Server Driver][SQL Server]语句已终止。"
[size=; font-size:12pt,12pt][font='Consolas']Error - RtlWerpReportException failed with status code :-1073741823. Will try to launch the process directly
[size=; font-size:12pt,12pt][font='Consolas']The program has unexpectedly finished.



离线sjyzhxw
只看该作者 1楼 发表于: 2012-05-25
额,问题解决了。。。
快速回复
限100 字节
 
上一个 下一个