这是我的第一个Android / sql项目,正在创建带有两个数据库的出勤应用程序。 DatabaseHelper有一个包含学生/教授列表的表。另一个名为coursesDatabase的表有一个存储类名称及其信息的表。我遇到的问题是,我想为记录该特定班级出勤率的每个班级创建表格,从COL1 = STUDENT_NAME开始,然后添加每个COL2 = LAST_NAME,COL3 = DATE1,COL4 = DATE2等。 。每次教授创建出勤部分时都会创建每个日期列。这是我的代码,不包括无关的完整方法:
public class CoursesDatabase extends SQLiteOpenHelper {
private static CoursesDatabase cd = null;
public static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME = "course_manager.db";
public static final String COURSES_TABLE_NAME = "COURSES";
public static final String KEY_ID = "ID"; //column index 0
public static final String KEY_NAME= "COURSE_NAME"; //column index 1
public static final String KEY_CODE = "COURSE_CODE"; //column index 2
public static final String KEY_HOUR = "COURSE_HOUR"; //column index 3
public static final String KEY_MINUTE = "COURSE_MINUTE"; //column index 4
public static final String KEY_INSTRUCTOR = "COURSE_INSTRUCTOR"; //column index 5
public static final String IS_ON = "IS_ON"; //column index 6
private CoursesDatabase(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
deleteAll();
addCourse(new Course("354", "Software Engineering", "12345", 10, 30, "doctor doc"));
addCourse(new Course("237", "CompSci 2", "13346", 11, 0, "john doe"));
}
public static CoursesDatabase getInstance(Context context){
if (cd == null){
cd = new CoursesDatabase(context);
}
return cd;
}
@Override
public void onCreate(SQLiteDatabase cd) {
final String SQL_CREATE_USER_TABLE = "CREATE TABLE " + COURSES_TABLE_NAME + "(" +
KEY_ID + " TEXT NOT NULL, " + // column 0
KEY_NAME + " TEXT NOT NULL, " + // column 1
KEY_CODE + " TEXT NOT NULL, " + // column 2
KEY_HOUR + " INT, " + // column 3
KEY_MINUTE + " INT, " + // column 4
KEY_INSTRUCTOR + " TEXT," + // column 5
IS_ON + "INT DEFAULT 0);"; // column 6
cd.execSQL(SQL_CREATE_USER_TABLE);
}
//Creates a table to log attendance for each class
String KEY_STUDENT_FIRST_NAME = "First_Name";
String KEY_STUDENT_LAST_NAME = "Last_Name";
public void newClassTable(SQLiteDatabase cd, Course course){
String CLASS_TABLE_NAME = course.getId() + "-" + course.getName() + "-" + course.getHour() + ":" + course.getMinute();
final String SQL_CREATE_CLASS_TABLE = "CREATE TABLE " + CLASS_TABLE_NAME + "(" +
KEY_STUDENT_FIRST_NAME + " STRING NOT NULL, " + // column 0
KEY_STUDENT_LAST_NAME + " STRING NOT NULL);"; // column 1
cd.execSQL(SQL_CREATE_CLASS_TABLE);
}
//takes the attendance of a student in course table. If the student does not exist in the course table, the student is then added.
public boolean attendStudentInCourse(Student student, Course course) {
SQLiteDatabase cd = this.getWritableDatabase();
//if professor has not turned on the course yet exit
if (!course.isAvailable()) {
return false;
}
return true;
}
所以我的问题是,当教授单击该课程的“新会话”时,我该使用什么方法在课程表的末尾创建一个新列?当学生尝试使用attendStudentInCourse()
方法时,如何更新最后一行?
答案 0 :(得分:0)
所以我的问题是我应该使用什么方法来创建新的 教授单击“新 课程”?
您可以使用
将列添加到表中(在某些条件下)ALTER TABLE <your_table_name> ADD COLUMN <you_column_definition(e)>
SQL As Understood By SQLite - ALTER TABLE 阅读此内容以了解条件/限制。
当出现以下情况时,我将如何更新该特定的最后一行? 学生尝试使用AttStudentInCourse()方法?
假设您的意思是列而不是行
请注意,您可以使用DEFAULT <default_value>
设置一个默认值,该默认值将应用于所有现有行。
如果设置默认值(我猜想一个新的会话最初在课程中将有0个学生)不适合,则可以使用UPDATE查询。
如果您要的是行,则:-
但是,添加列不会添加行,添加的列将应用于所有行。
说所有设计可能都可以改进为:-
作为示例,也许考虑使用以下内容:-
-注意具有多个表的单个数据库,这些表具有关系映射又称为引用
-注意具有多个表的单个数据库,这些表具有关系映射(又称为引用)
DROP TABLE IF EXISTS role;
DROP TABLE IF EXISTS person;
DROP TABLE IF EXISTS course;
DROP TABLE IF EXISTS attendance;
DROP VIEW IF EXISTS proposed_attendance_list;
DROP VIEW IF EXISTS non_attendance_list;
-- Create the role table
CREATE TABLE IF NOT EXISTS role (_id INTEGER PRIMARY KEY, role);
-- Add some roles
-- NOTE _id column is not specified so SQLite will assign a unqiue value
-- assigned value will likely be 1, then likely 2, then likely 3........9,223,372,036,854,775,807 (highest)
-- NOTE no need for AUTOINCREMENT keyword and the resultant performance loss
-- NOTE _id is the column name that best suits Android (e.g. Cursor Adapters need this column name)
INSERT INTO ROLE (role) VALUES
('Instructor'), -- id 1
('Student'), -- id 2
('Instructor Aid '), -- id 3
('Administrator') -- id 4
-- !!!NOTE!!! you would not typically assume specific id
;
-- Create the Person table (Person will have a role - for simplicity of demo single role assumed)
CREATE TABLE IF NOT EXISTS person (_id INTEGER PRIMARY KEY, name TEXT, role INTEGER);
INSERT INTO person (name,role) VALUES
('Professor Plum',1), -- Instructor id = 1
('Tom Brown',2), -- Student id = 2
('Mary Smith',2), -- Student id = 3
('Sue Barnes',2), -- Student id = 4
('Ty Pit',4), -- Admin id = 5
('Dr. J M Hardy',1), -- Instructor id = 6
('Matilda Dance',2), -- Student id = 7
('Fred Bloggs',3) -- Aid id = 8
;
CREATE TABLE IF NOT EXISTS course (
_id INTEGER PRIMARY KEY,
course_name TEXT NOT NULL,
course_code TEXT NOT NULL,
course_hour TEXT NOT NULL,
course_minute TEXT,
course_time,
course_instructor INTEGER
);
INSERT INTO course (course_name, course_code, course_hour, course_minute, course_instructor) VALUES
('MATHS BASIC','M001','10','30',6), -- Maths with instructor Dr. J M Hardy id = 1
('MATHS APPLIED', 'M101','12','30',6), -- Applied Maths instructor Dr. J M Hardy id =2
('MATHS ADVANCED','M011','11','00',1) -- Advanced Maths instructor Professor Plum id =3
;
CREATE TABLE IF NOT EXISTS attendance (course_reference INTEGER, person_reference INTEGER, attended INTEGER, PRIMARY KEY (course_reference, person_reference));
INSERT INTO attendance VALUES
(1,6,0), -- Dr J M Hardy should attend (attended 0 = should attend, -1 if did not attend, 1 if did attend) course MATCHS BASIC
(1,3,0), -- Mary Should attent Basic maths
(1,7,0), -- Matilda should attend basic maths
(1,8,0), -- Aid Fred should attend basic maths
(3,1,1), -- Advanced maths Plum attended (taught it)
(3,2,-1), -- Advanced maths Tom Brown missed
(3,3,1), -- Advanced maths mary attended
(3,8,1), -- Aid Fred attended
(2,4,1), -- Applied Maths Sue attended
-- (2,4,0) WOULD BE DUPLICATE SO NOT ALLOWED (INSERT OR IGNORE would skip, Note android SQliteDatabase insert method uses INSERT OR IGNORE)
(2,3,0), -- Mary didn't attend as such but not flagged as did not attend (could just use 0 to indicate did not attend)
(2,7,1), -- Applied Maths Matilda attended
(2,6,0), -- Dr Hardy should attend
(2,1,0), -- Prof Plum should attend
(2,8,0) -- Fred Bloggs Admin should attend
;
-- Course attendace example listing the course details, the number of attendees and the list of attendees
-- Create as a view so that it can be resued (only need to type it out once)
CREATE VIEW IF NOT EXISTS proposed_attendance_list AS
SELECT
course.course_name,
course.course_code,
course.course_hour||':'||course.course_minute AS CourseTime,
count(attended) AS proposed_attendance,
group_concat(Person.name,' ,') AS peeople
FROM Course
JOIN attendance ON course._id = attendance.course_reference
JOIN person ON attendance.person_reference = person._id
JOIN role ON person.role = role._id
-- GROUP BY course_hour, course_minute
-- HAVING
GROUP BY course_code
ORDER BY course.course_code,role._id
;
-- Initial proposed course attendance
SELECT * FROM proposed_attendance_list;
-- Change some course codes
UPDATE course SET course_code = 'M201' WHERE course_code = 'M101';
UPDATE course SET course_code = 'M111' WHERE course_code = 'M011';
-- Modifed proposed course attendance
SELECT * FROM proposed_attendance_list;
首先要写一个无人参与演示的信
CREATE VIEW IF NOT EXISTS non_attendance_list AS
SELECT course_name,
course_code,
Person.name,
Person.role,
'Dear '||person.name||' our records show that you did''t attend Course '||course_code||' '||course_name||' rest of letter' AS letter_to_send
FROM attendance
JOIN course ON course_reference = course._id
JOIN Person ON person_reference = Person._id
JOIN Role ON Person.role = role._id
WHERE attendance.attended < 1
ORDER BY course_code
;
-- Initial letters
SELECT letter_to_send FROM non_attendance_list;
-- Set Mary as having attended M001 Basic maths
UPDATE attendance SET attended = 1
WHERE course_reference = (SELECT _id FROM course WHERE course_code =
'M001' -- value that would be changed to suit
)
AND person_reference = (SELECT _id FROM person WHERE name =
'Mary Smith' -- value that would be changed to suit
)
;
-- Letters after update
SELECT letter_to_send FROM non_attendance_list;