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; /*from w ww. j ava2 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.Exam; public class ExamTable extends DAOHelper { private String TABLE_NAME = "exam"; /** * Constructor * @param context */ public ExamTable(Context context) { super(context); } /** * Constructor * @param context * @param name * @param factory * @param version */ public ExamTable(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 ExamTable(Context context, String name, CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { super(context, name, factory, version, errorHandler); } /** * Check if database contains student'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.i("Error", "Table not exist"); } mDatabase.close(); super.closeDB(); return tableExists; } /** * Get exam from exam's table by id * @param id * @return exam */ public Exam GetExamById(Long id){ Exam exam = new Exam(0, 0, null, null, null); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME + " WHERE id = ?"; Cursor cursor = super.db.rawQuery(sql, new String[]{id.toString()}); cursor.moveToFirst(); exam = LoadFromCursor(cursor); cursor.close(); super.closeDB(); return exam; } /** * Get exam's list from exam's table by id * @return studentsList */ public List<Exam> GetDataFromTable() { List<Exam> examsList = new ArrayList<Exam>(); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME; Cursor cursor = super.db.rawQuery(sql, null); cursor.moveToFirst(); while (cursor.isAfterLast() == false) { Exam exam = LoadFromCursor(cursor); examsList.add(exam); cursor.moveToNext(); } cursor.close(); super.closeDB(); return examsList; } /** * Get exam's initialized object by cursor * @param cursor * @return student */ private Exam LoadFromCursor(Cursor cursor) { long _id = cursor.getLong(cursor.getColumnIndex("id")); long _idSubject = cursor.getLong(cursor.getColumnIndex("id_subject")); String _classroom = cursor.getString(cursor.getColumnIndex("classroom")); String _date = cursor.getString(cursor.getColumnIndex("date")); String _time = cursor.getString(cursor.getColumnIndex("time")); return new Exam(_id, _idSubject, _classroom, _date, _time); } /** * Delete exam by id * @param id */ public void DeleteById(Long id){ super.openWritableDB(); super.db.delete(TABLE_NAME, "id = ?", new String[]{id.toString()}); super.closeDB(); } /** * Insert exam's object information into database * @param exam */ public void Insert(Exam exam){ super.openWritableDB(); ContentValues data = new ContentValues(); data.put("id_subject", exam.getSubject()); data.put("date", exam.getDate()); data.put("time", exam.getTime()); data.put("classroom", exam.getClassroom()); super.db.insert(TABLE_NAME, null, data); super.closeDB(); } /** * Update exam's object information into database * @param exam */ public void UpdateTableById(Exam exam){ super.openWritableDB(); ContentValues data = new ContentValues(); data.put("id_subject", exam.getSubject()); data.put("date", exam.getDate()); data.put("time", exam.getTime()); data.put("classroom", exam.getClassroom()); super.db.update(TABLE_NAME, data, "id=" + exam.getId(), null); super.closeDB(); } /** * Delete exam by subject id * @param id */ public void DeleteBySubjectId(Long id) { super.openWritableDB(); super.db.delete(TABLE_NAME, "id_subject = ?", new String[]{id.toString()}); super.closeDB(); } }