SQLite“错误:找不到列'studentID'”

时间:2013-04-14 21:28:31

标签: sqlite

当我尝试编写一个简单的数据库来代表小学时,我在尝试编译代码时遇到了这个错误。

当我尝试INSERT到ATTENDS表时发生错误。

我似乎无法找到错误,因为其他一切正常。删除所有触发器仍会导致错误,因此它们不是问题。

任何帮助都会非常感激。

CREATE TABLE IF NOT EXISTS Student(
    id INTEGER PRIMARY KEY AUTOINCREMENT, 
    name TEXT NOT NULL, 
    lastName TEXT NOT NULL, 
    gender TEXT COLLATE NOCASE NOT NULL ,
    DOB TEXT NOT NULL, 
    address TEXT NOT NULL, 
    grade TEXT NOT NULL,
    /* Checks */
    CHECK(gender IN ("m", "f", "o")),
    CHECK(grade IN("K", "1", "2", "3", "4", "5", "6"))
);
CREATE TABLE IF NOT EXISTS Course(
    id INTEGER PRIMARY KEY AUTOINCREMENT, 
    name TEXT NOT NULL, 
    grade TEXT COLLATE NOCASE NOT NULL,
    /* Checks */
    CHECK(grade IN("K", "1", "2", "3", "4", "5", "6"))
);

CREATE TABLE IF NOT EXISTS Attends(
    studentID INTEGER,
    courseID INTEGER,
    gradePoints TEXT COLLATE NOCASE,
    FOREIGN KEY(studentID) REFERENCES Student(id), 
    FOREIGN KEY(courseID) REFERENCES Course(id), 
    /* Checks */
    CHECK(gradePoints ISNULL OR gradePoints IN ("A", "B", "C", "D", "E")),
    UNIQUE(studentID, courseID)
);


CREATE TRIGGER IF NOT EXISTS sameGrade 
BEFORE INSERT ON Attends 
WHEN (SELECT grade  FROM Student WHERE Student.id = studentID) <> (SELECT grade
FROM Course Where Course.id = courseID)
BEGIN
    SELECT RAISE(ABORT,'The student’s grade does not match the course’s grade.');
END;

CREATE TRIGGER IF NOT EXISTS changeGrade
BEFORE UPDATE ON Student
WHEN (NOT OLD.grade LIKE "K" AND NEW.grade LIKE "K") OR (NOT OLD.grade LIKE "K" AND NEW.grade < OLD.grade) 
BEGIN
    SELECT RAISE(ABORT,'The student cannot go into a lower grade.');
END;

CREATE TRIGGER IF NOT EXISTS max100Students
BEFORE INSERT ON Attends
WHEN 100 < (SELECT COUNT(*) FROM Attends WHERE courseID = NEW.courseID)
BEGIN
    SELECT RAISE(ABORT,'Courses cannot have more than 100 students.');
END;


INSERT INTO Student(name,lastName,gender,DOB,address,grade) VALUES('Taylor','B','F','1992-04-25','123 1st ST','2');
INSERT INTO Student(name,lastName,gender,DOB,address,grade) VALUES('Taylor','B','F','1992-04-25','123 1st ST','2');
INSERT INTO Student(name,lastName,gender,DOB,address,grade) VALUES('Taylor','B','F','1992-04-25','123 1st ST','2');
INSERT INTO Student(name,lastName,gender,DOB,address,grade) VALUES('Taylor','B','F','1992-04-25','123 1st ST','2');
INSERT INTO Student(name,lastName,gender,DOB,address,grade) VALUES('Taylor','B','F','1992-04-25','123 1st ST','4');


INSERT INTO Course(name,grade) VALUES('Gym','K');
INSERT INTO Course(name,grade) VALUES('Gym','1');
INSERT INTO Course(name,grade) VALUES('Gym','2');
INSERT INTO Course(name,grade) VALUES('Gym','3');
INSERT INTO Course(name,grade) VALUES('Gym','4');
INSERT INTO Course(name,grade) VALUES('Gym','5');
INSERT INTO Course(name,grade) VALUES('Gym','6');

INSERT INTO Attends(studentID, courseID, gradePoints) VALUES(1,3,NULL);
INSERT INTO Attends(studentID, courseID, gradePoints) VALUES(2,3,NULL);
INSERT INTO Attends(studentID, courseID, gradePoints) VALUES(3,3,NULL);
-- INSERT INTO Attends(studentID, courseID, gradePoints) VALUES(4,3,NULL);
-- INSERT INTO Attends(studentID, courseID, gradePoints) VALUES(5,3,NULL);

1 个答案:

答案 0 :(得分:1)

问题出在sameGrade触发器中。您忘记使用NEW引用该行。只是在“新”之前。到“studentID”和“courseID”列,以便sqlite3知道你所指的是:

CREATE TRIGGER IF NOT EXISTS sameGrade 
BEFORE INSERT ON Attends 
WHEN (SELECT grade FROM Student WHERE Student.id = NEW.studentID) <> (SELECT grade FROM Course WHERE Course.id = NEW.courseID)
BEGIN
    SELECT RAISE(ABORT,'The student’s grade does not match the course’s grade.');
END;