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 w w . ja v a 2 s . com 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.SubjectMatter; public class SubjectMatterTable extends DAOHelper { private String TABLE_NAME = "subject_matter"; /** * Constructor * @param context */ public SubjectMatterTable(Context context) { super(context); } /** * Constructor * @param context * @param name * @param factory * @param version */ public SubjectMatterTable(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 SubjectMatterTable(Context context, String name, CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { super(context, name, factory, version, errorHandler); } /** * Check if database contains subject-matter'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 subject's matter list from subject-matter's table by id * @param id * @return subMatterList */ public List<SubjectMatter> GetDataFromTableBySubjectId(Long id){ List<SubjectMatter> subMatterList = new ArrayList<SubjectMatter>(); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME + " WHERE subject_id = ?"; Cursor cursor = super.db.rawQuery(sql, new String[]{id.toString()}); cursor.moveToFirst(); while (cursor.isAfterLast() == false) { SubjectMatter a = LoadFromCursor(cursor); subMatterList.add(a); cursor.moveToNext(); } cursor.close(); super.closeDB(); return subMatterList; } /** * Get subject's matter list from subject-matter's table * @return subMatterList */ public List<SubjectMatter> GetDataFromTable() { List<SubjectMatter> subMatterList = new ArrayList<SubjectMatter>(); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME; Cursor cursor = super.db.rawQuery(sql, null); cursor.moveToFirst(); while (cursor.isAfterLast() == false) { SubjectMatter a = LoadFromCursor(cursor); subMatterList.add(a); cursor.moveToNext(); } cursor.close(); super.closeDB(); return subMatterList; } /** * Get matter's initialized object by cursor * @param cursor * @return subjectMatter */ private SubjectMatter LoadFromCursor(Cursor cursor) { SubjectMatter subjectMatter = new SubjectMatter(); subjectMatter.id = cursor.getLong(cursor.getColumnIndex("id")); subjectMatter.name = cursor.getString(cursor.getColumnIndex("name")); subjectMatter.position = cursor.getInt(cursor.getColumnIndex("position")); return subjectMatter; } /** * 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, "subject_id = ?", new String[]{sub_id.toString()}); super.closeDB(); } /** * Insert matter's object information into database * @param smt */ public void Insert(SubjectMatter smt){ super.openWritableDB(); ContentValues data = new ContentValues(); data.put("name", smt.name); data.put("subject_id", smt.subID); data.put("position", smt.position); super.db.insert(TABLE_NAME, null, data); super.closeDB(); } }