【发布时间】:2018-09-16 00:09:06
【问题描述】:
我有这段代码 sn-p 调用数据库表,我将在其中执行一些操作,例如分配房间,字段 HostelName 为 NULL。
但是在尝试了许多方法后,我的代码中并没有真正观察到 NULL 字段,即使该字段为 NULL,执行总是进入第一个 IF 语句。真的有办法检查一个字段是否为 NULL 并相应地执行一些操作吗?我在 Qt 和数据库方面有点新手。
注意:
MatricNo 是主键。显然 SELECT 语句只能返回一条记录(我猜)。
qry.prepare("SELECT * from student where matricno='"+matric+"' and HostelName IS NULL");
qry.exec();
QSqlRecord rec = qry.record();
if(rec.isNull("HostelName"))
{
qry.prepare("UPDATE student set HostelName='"+hostelName+"',RoomNo='"+roomNo+"' where MatricNo='"+matric+"'");
if(qry.exec())
{
qry.prepare("UPDATE Rooms set OccupantsNo=OccupantsNo-1 where HostelName='"+hostelName+"' and roomID='"+roomNo+"'");
if(qry.exec())
{
QMessageBox::critical(this,tr("Error"),tr("Student Room Allocated"));
}
else
{
QMessageBox::critical(this,tr("Error"),qry.lastError().text());
}
}
else
{
QMessageBox::critical(this,tr("Error"),qry.lastError().text());
}
}
else
{
QMessageBox::critical(this,tr("Error"),"Cant Allocate Room: \nStudent Room Already Alloted\nTo Re-Allocate, Go to Edit Student Details");//qry.lastError().text());
}
【问题讨论】:
-
你可以提供一个minimal reproducible example,如果数据库是sqlite的.db文件