gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:58:58 24 4
gpt4 key购买 nike

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

当我尝试将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);

最佳答案

问题出在同一个Grade触发器中。您忘记了使用NEW引用该行。只需添加“ 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;

关于sqlite - SQLite“错误:找不到列'studentID'”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16004524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com