我正在创建一个Android应用程序,其功能是登录并参加会议。它有两个表,一个包含所有类及其详细信息的COURSES表,以及对应于COURSES中每一行的CLASS表。每个CLASS表使用COL1:FIRST_NAME,COL:2LAST_NAME记录出勤情况,然后在被调用时动态创建COL3,COL4等,以表示当天特定学生的出勤情况。 公共课程CoursesDatabase扩展了SQLiteOpenHelper { 私有静态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
public static final String LATEST_TIME = "LATEST_TIME"; //column index 7
private CoursesDatabase(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
deleteAll();
addCourse(new Course("SE_354", "Software_Engineering", "12345", 10, 30, "John Doe 1"));
addCourse(new Course("CE_237", "CompSci_2", "13346", 11, 0, "john doe2"));
}
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
LATEST_TIME + "String);"; // column 7
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(Course course){
SQLiteDatabase cd = this.getWritableDatabase();
String CLASS_TABLE_NAME = course.getId() + "_" + course.getName() + "_" + course.getHour() + course.getMinute();//needs refactoring
final String SQL_CREATE_CLASS_TABLE = "CREATE TABLE IF NOT EXISTS " + CLASS_TABLE_NAME + "(" +
KEY_STUDENT_FIRST_NAME + " STRING, " + // column 0
KEY_STUDENT_LAST_NAME + " STRING);"; // column 1
cd.execSQL(SQL_CREATE_CLASS_TABLE);
}
public boolean addSession(Course course)
{
SQLiteDatabase cd = this.getWritableDatabase();
//current date in numerical format, e.g. 08/12/18
String CLASS_TABLE_NAME = course.getId() + "_" + course.getName() + "_" + course.getHour() + course.getMinute();//needs refactoring
cd.execSQL("ALTER TABLE " + CLASS_TABLE_NAME + " ADD COLUMN " + "'" + course.getLatestTime() + "'" + " INT;");
return true;
}
//****NEEDS TESTING ****
//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(User student, Course course) {
SQLiteDatabase cd = this.getWritableDatabase();
String CLASS_TABLE_NAME = course.getId() + "_" + course.getName() + "_" + course.getHour() + course.getMinute();//needs refactoring
//if professor has not turned on the course yet exit
if (!course.isAvailable()) {
return false;
}
//if student has never attended course before, add their row
Cursor cursor = cd.rawQuery("SELECT * FROM " + CLASS_TABLE_NAME + " WHERE First_Name = ? AND Last_Name = ? ",
new String[]{student.getFirst_name(),student.getLast_name()});
if (!cursor.moveToNext())
{
ContentValues studentvalues = new ContentValues();
studentvalues.put(KEY_STUDENT_FIRST_NAME, student.getFirst_name());
studentvalues.put(KEY_STUDENT_LAST_NAME, student.getLast_name());
cd.insert(CLASS_TABLE_NAME, null, studentvalues);
}
//otherwise, just tick their attendance on the current date
ContentValues datevalue = new ContentValues();
datevalue.put("'" + course.getLatestTime() + "'", 1);
cd.update(CLASS_TABLE_NAME, datevalue, KEY_STUDENT_FIRST_NAME+" = ? AND " + KEY_STUDENT_LAST_NAME+" = ?", new String [] {student.getFirst_name(), student.getLast_name()});
return true;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + COURSES_TABLE_NAME);
onCreate(db);
}
我的问题是如何将特定的CLASS表转换为excel文件(保留列/行格式),然后将其作为电子邮件发送到教授的电子邮件地址?一般的批评也很有帮助,我意识到这可能是构建表的一种糟糕方法,但这是我第一次接触Android Studio和数据库。这种实现对我来说很有意义,我现在不太担心性能效率。
编辑:使用工作版本更新代码。