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 ww w. ja va 2 s . c om 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.Subject; public class SubjectTable extends DAOHelper { private String TABLE_NAME = "subject"; /** * Constructor * @param context */ public SubjectTable(Context context) { super(context); } /** * Constructor * @param context * @param name * @param factory * @param version */ public SubjectTable(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } /** * Constuctor * @param context * @param name * @param factory * @param version * @param errorHandler */ public SubjectTable(Context context, String name, CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { super(context, name, factory, version, errorHandler); } /** * Check if database contains 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.i("Error", "Table not exist"); } mDatabase.close(); super.closeDB(); return tableExists; } /** * Get subject by id * @param id * @return subject */ public Subject GetSubjectById(Long id){ Subject subject = new Subject(); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME + " WHERE id = ?"; Cursor cursor = super.db.rawQuery(sql, new String[]{id.toString()}); cursor.moveToFirst(); while (cursor.isAfterLast() == false) { subject = LoadFromCursor(cursor); cursor.moveToNext(); } cursor.close(); super.closeDB(); return subject; } /** * Get id by name * @param name * @return id */ public Long GetIdByName(String name){ Subject subject = new Subject(); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME + " WHERE name = ?"; Cursor cursor = super.db.rawQuery(sql, new String[]{name}); cursor.moveToFirst(); subject = LoadFromCursor(cursor); cursor.close(); super.closeDB(); return subject.id; } /** * Get subject lists from subject's table * @return subjectList */ public List<Subject> GetDataFromTable() { List<Subject> subjectList = new ArrayList<Subject>(); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME; Cursor cursor = super.db.rawQuery(sql, null); cursor.moveToFirst(); while (cursor.isAfterLast() == false) { Subject a = LoadFromCursor(cursor); subjectList.add(a); cursor.moveToNext(); } cursor.close(); super.closeDB(); return subjectList; } /** * Get subject's names list from subject's table * @return subjectList */ public List<String> GetNamesFromTable() { List<String> subjectList = new ArrayList<String>(); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME; Cursor cursor = super.db.rawQuery(sql, null); cursor.moveToFirst(); while (cursor.isAfterLast() == false) { String a = LoadNameFromCursor(cursor); subjectList.add(a); cursor.moveToNext(); } cursor.close(); super.closeDB(); return subjectList; } /** * Get cursor by subject's name * @param cursor * @return cursor */ private String LoadNameFromCursor(Cursor cursor) { return cursor.getString(cursor.getColumnIndex("name")); } /** * Get subject's initialized object by cursor * @param cursor * @return subject */ private Subject LoadFromCursor(Cursor cursor) { Subject subject = new Subject(); subject.id = cursor.getLong(cursor.getColumnIndex("id")); subject.name = cursor.getString(cursor.getColumnIndex("name")); subject.description = cursor.getString(cursor.getColumnIndex("description")); return subject; } /** * Delete subject by id * @param id */ public void DeleteById(Long id){ super.openWritableDB(); super.db.delete(TABLE_NAME, "id = ?", new String[]{id.toString()}); super.closeDB(); } /** * Insert subject's object information into database * @param s * @return id */ public long Insert(Subject s){ super.openWritableDB(); ContentValues data = new ContentValues(); data.put("name", s.name); data.put("description", s.description); long id = super.db.insert(TABLE_NAME, null, data); super.closeDB(); return id; } }