Back to project page digitalcampus.
The source code is released under:
MIT License
If you think the Android project digitalcampus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.llenguatges.digitalcampus.database; // w w w . ja v a2 s .c o m import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.DatabaseErrorHandler; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.util.Log; import com.llenguatges.digitalcampus.objects.StudentSubject; public class StudentSubjectTable extends DAOHelper { private String TABLE_NAME = "stu_sub"; /** * Constructor * @param context */ public StudentSubjectTable(Context context) { super(context); } /** * Constructor * @param context * @param name * @param factory * @param version */ public StudentSubjectTable(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } /** * Constructor * @param context * @param name * @param factory * @param version * @param errorHandler */ public StudentSubjectTable(Context context, String name, CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { super(context, name, factory, version, errorHandler); } /** * Check if database contains student-subject's table * @return tableExists */ public Boolean isTableExist(){ openReadableDB(); SQLiteDatabase mDatabase = super.db; @SuppressWarnings("unused") Cursor c = null; boolean tableExists = false; try{ c = mDatabase.query(TABLE_NAME, null, null, null, null, null, null); tableExists = true; } catch (Exception e) { Log.e("Error", "Table not exist"); } mDatabase.close(); super.closeDB(); return tableExists; } /** * Get subject id's list from student-subject's table by student id * @param id * @return subIdList */ public List<Long> GetSubjectIdFromTableByStudentId(Long id) { List<Long> subIdList = new ArrayList<Long>(); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME + " WHERE id_student = ?"; Cursor cursor = super.db.rawQuery(sql, new String[]{id.toString()}); cursor.moveToFirst(); while (cursor.isAfterLast() == false) { Long a = LoadSubjectIdFromCursor(cursor); subIdList.add(a); cursor.moveToNext(); } cursor.close(); super.closeDB(); return subIdList; } /** * Get student id's list from student-subject's table by subject id * @param id * @return stuIdList */ public List<Long> GetStudentIdFromTableBySubjectId(Long id) { List<Long> stuIdList = new ArrayList<Long>(); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME + " WHERE id_subject = ?"; Cursor cursor = super.db.rawQuery(sql, new String[]{id.toString()}); cursor.moveToFirst(); while (cursor.isAfterLast() == false) { Long a = LoadStudentIdFromCursor(cursor); stuIdList.add(a); cursor.moveToNext(); } cursor.close(); super.closeDB(); return stuIdList; } /** * Get cursor by student's id * @param cursor * @return cursor */ private Long LoadStudentIdFromCursor(Cursor cursor) { return cursor.getLong(cursor.getColumnIndex("id_student")); } /** * Get cursor by subject's id * @param cursor * @return cursor */ private Long LoadSubjectIdFromCursor(Cursor cursor) { return cursor.getLong(cursor.getColumnIndex("id_subject")); } /** * Get student lists from student-subject's table * @return subjectList */ public List<StudentSubject> GetDataFromTable() { List<StudentSubject> stuSubList = new ArrayList<StudentSubject>(); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME; Cursor cursor = super.db.rawQuery(sql, null); cursor.moveToFirst(); while (cursor.isAfterLast() == false) { StudentSubject a = LoadFromCursor(cursor); stuSubList.add(a); cursor.moveToNext(); } cursor.close(); super.closeDB(); return stuSubList; } /** * Get matter's initialized object by cursor * @param cursor * @return stuSub */ private StudentSubject LoadFromCursor(Cursor cursor) { StudentSubject stuSub = new StudentSubject(); stuSub.id = cursor.getLong(cursor.getColumnIndex("id")); stuSub.stuID = cursor.getLong(cursor.getColumnIndex("id_student")); stuSub.subID = cursor.getLong(cursor.getColumnIndex("id_subject")); return stuSub; } /** * Delete matter by id * @param id */ public void DeleteById(Long id){ super.openWritableDB(); super.db.delete(TABLE_NAME, "id = ?", new String[]{id.toString()}); super.closeDB(); } /** * Delete matter by subject id * @param sub_id */ public void DeleteBySubjectID(Long sub_id){ super.openWritableDB(); super.db.delete(TABLE_NAME, "id_subject = ?", new String[]{sub_id.toString()}); super.closeDB(); } /** * Delete matter by student id * @param stu_id */ public void DeleteByStudentID(Long stu_id){ super.openWritableDB(); super.db.delete(TABLE_NAME, "id_student = ?", new String[]{stu_id.toString()}); super.closeDB(); } /** * Insert student and subject's id information into database * @param stSb */ public void Insert(StudentSubject stSb){ super.openWritableDB(); ContentValues data = new ContentValues(); data.put("id_subject", stSb.subID); data.put("id_student", stSb.stuID); super.db.insert(TABLE_NAME, null, data); super.closeDB(); } }