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 . j a v a2s. c o m*/ import java.io.ByteArrayOutputStream; 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.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import com.llenguatges.digitalcampus.objects.Student; public class StudentTable extends DAOHelper { private String TABLE_NAME = "student"; /** * Constructor * @param context */ public StudentTable(Context context) { super(context); } /** * Constructor * @param context * @param name * @param factory * @param version */ public StudentTable(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 StudentTable(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 students from subject-matter's table by id * @param id * @return student */ public Student GetStudentById(Long id){ Student student = new Student(id, null, null, null, null, null, false, null); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME + " WHERE id = ?"; Cursor cursor = super.db.rawQuery(sql, new String[]{id.toString()}); cursor.moveToFirst(); student = LoadFromCursor(cursor); cursor.close(); super.closeDB(); return student; } /** * Get subject's matter list from subject-matter's table by id * @return studentsList */ public List<Student> GetDataFromTable() { List<Student> studentsList = new ArrayList<Student>(); super.openReadableDB(); String sql = "SELECT * FROM " + TABLE_NAME; Cursor cursor = super.db.rawQuery(sql, null); cursor.moveToFirst(); while (cursor.isAfterLast() == false) { Student a = LoadFromCursor(cursor); studentsList.add(a); cursor.moveToNext(); } cursor.close(); super.closeDB(); return studentsList; } /** * Get student's initialized object by cursor * @param cursor * @return student */ private Student LoadFromCursor(Cursor cursor) { long _id = cursor.getLong(cursor.getColumnIndex("id")); String _firstName = cursor.getString(cursor.getColumnIndex("first_name")); String _lastName = cursor.getString(cursor.getColumnIndex("last_name")); String _age = cursor.getString(cursor.getColumnIndex("date_birth")); String _description = cursor.getString(cursor.getColumnIndex("specialty")); String _gender = cursor.getString(cursor.getColumnIndex("sex")); byte[] img = cursor.getBlob(cursor.getColumnIndex("photo")); Bitmap _photo; if(img != null){ _photo = BitmapFactory.decodeByteArray(img, 0, img.length); }else{ _photo = null; } return new Student(_id, _firstName, _lastName, _age, _description, _gender, false, _photo); } /** * Delete student by id * @param id */ public void DeleteById(Long id){ super.openWritableDB(); super.db.delete(TABLE_NAME, "id = ?", new String[]{id.toString()}); super.closeDB(); } /** * Insert student's object information into database * @param s * @return id */ public long Insert(Student s){ super.openWritableDB(); ContentValues data = new ContentValues(); data.put("first_name", s.getFirstName()); data.put("last_name", s.getLastName()); data.put("specialty", s.getDescription()); data.put("date_birth", s.getAge()); data.put("sex", s.getGender()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); s.getPhoto().compress(Bitmap.CompressFormat.JPEG, 100, bos); byte[] img =bos.toByteArray(); data.put("photo", img); long id = super.db.insert(TABLE_NAME, null, data); super.closeDB(); return id; } }