2013-04-14 47 views
2

當我試圖編寫一個簡單的數據庫來代表一所小學時,我在編譯代碼時遇到了這個錯誤。SQLite「錯誤:找不到列'studentID'」

當我嘗試插入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

什麼是錯誤? – 2013-04-14 22:55:46

+1

你的學生表沒有StudentId Cloumn並有Id。這是合理的錯誤 –

+1

我試圖添加到Attends表,雖然哪裏有一個StudentID列 – user2280563

回答

1

問題出在同一級別的觸發器上。您忘記使用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;