- void AdministratorMainWindow::insertCourse()
- {
- bool successful=false;
- QMessageBox *errorMsg=0;
- if(courseIDLine->text ().isEmpty () || courseNameLine->text().isEmpty ()
- ||courseTeacherIDLine->text ().isEmpty ())
- {
- errorMsg=new QMessageBox(QMessageBox::Information,tr("添加失败!"),
- tr("请填满信息"),QMessageBox::Ok,this);
- errorMsg->show();
- }
- else {
-
- //不符合外键会直接退出!
- query->prepare ("insert into Courses values (?,?,?)");
- query->bindValue (0,courseIDLine->text ());
- query->bindValue (1,courseNameLine->text ());
- query->bindValue (2,courseTeacherIDLine->text ());
- successful=query->exec ();
- }if(successful)
- {
- closeInsertCourseDlg ();
- }else if(!errorMsg->isVisible ())
- {
- errorMsg=new QMessageBox(QMessageBox::Information,tr("添加失败!"),
- tr("无法添加课程信息,可能是因为所填教师不存在!"),QMessageBox::Ok,this);
- errorMsg->show();
- }
- }
本来指望如果出错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.