Back to project page ProjectStudio.
The source code is released under:
Apache License
If you think the Android project ProjectStudio 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 DB_Provider; //from w ww .j av a 2 s. c o m import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.sql.SQLException; import model.Professor; /** * Created by desmond on 1/23/14. */ public class ProfessorDataSource { //Database Fields private SQLiteDatabase database; private DBManager prof_dbHelper; private String[] allColumns = {DB_ABSTRACTS.DBProfessor.KEY_ID, DB_ABSTRACTS.DBProfessor.NAME_COLUMN, DB_ABSTRACTS.DBProfessor.EMAIL_COLUMN, DB_ABSTRACTS.DBProfessor.COURSE_COLUMN}; public ProfessorDataSource(Context context) { prof_dbHelper = new DBManager(context); } public void open() throws SQLException { database = prof_dbHelper.getWritableDatabase(); } public void close() { if (database != null && database.isOpen()) { prof_dbHelper.close(); } } /* INSERT A PROFESSOR INTO DATABASE */ public long createProfessor(Professor professor) { database = prof_dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(DB_ABSTRACTS.DBProfessor.NAME_COLUMN, professor.getProfessor_name()); values.put(DB_ABSTRACTS.DBProfessor.EMAIL_COLUMN, professor.getProfessor_mail()); values.put(DB_ABSTRACTS.DBProfessor.COURSE_COLUMN, professor.getProfessor_courses()); //INSERT ROW long id; id = database.insert(DB_ABSTRACTS.DBProfessor.DATABASE_TABLE, null, values); return id; } /* GET SINGLE PROFESSOR */ public Professor getProfessor(long professor_id) { database = prof_dbHelper.getReadableDatabase(); String where = DB_ABSTRACTS.DBProfessor.KEY_ID + " = " + professor_id; Cursor cursor = null; if (database != null) { cursor = database.query(DB_ABSTRACTS.DBProfessor.DATABASE_TABLE, allColumns, where, null, null, null, null); } Professor prof = new Professor(); prof.setId(cursor.getInt((cursor.getColumnIndex(DB_ABSTRACTS.DBProfessor.KEY_ID)))); prof.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBProfessor.NAME_COLUMN))); prof.setProfessor_mail(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBProfessor.EMAIL_COLUMN))); prof.setProfessor_course(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBProfessor.COURSE_COLUMN))); return prof; } /* GET ALL PROFESSORS */ public Cursor getAllProfessors() { database = prof_dbHelper.getReadableDatabase(); // List<Professor> professor_list = new ArrayList<Professor>(); Cursor cursor = database.query(DB_ABSTRACTS.DBProfessor.DATABASE_TABLE, allColumns, null, null, null, null, DB_ABSTRACTS.DBProfessor.NAME_COLUMN + " ASC"); //LOOP THROUGH ALL ROWS AND ADD TO THE LIST if (cursor.moveToFirst()) { do { Professor professors = new Professor(); professors.setId(cursor.getInt((cursor.getColumnIndex(DB_ABSTRACTS.DBProfessor.KEY_ID)))); professors.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBProfessor.NAME_COLUMN))); professors.setProfessor_mail(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBProfessor.EMAIL_COLUMN))); professors.setProfessor_course(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBProfessor.COURSE_COLUMN))); //ADD TO THE LIST //professor_list.add(professors); } while (cursor.moveToNext()); } //cursor.close(); //return professor_list; return cursor; } /* UPDATE A PROFESSOR */ public int updateProfessor(Professor professor) { database = prof_dbHelper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put(DB_ABSTRACTS.DBProfessor.NAME_COLUMN, professor.getProfessor_name()); values.put(DB_ABSTRACTS.DBProfessor.EMAIL_COLUMN, professor.getProfessor_mail()); values.put(DB_ABSTRACTS.DBProfessor.COURSE_COLUMN, professor.getProfessor_courses()); String where = DB_ABSTRACTS.DBProfessor.KEY_ID + " = ?"; //update row return database.update(DB_ABSTRACTS.DBProfessor.DATABASE_TABLE, values, where, new String[]{String.valueOf(professor.getId())}); } /* DELETE PROFESSOR */ public void deleteProfessor(long professor_id) { database = prof_dbHelper.getWritableDatabase(); String where = DB_ABSTRACTS.DBProfessor.KEY_ID + " = ?"; database.delete(DB_ABSTRACTS.DBProfessor.DATABASE_TABLE, where, new String[]{String.valueOf(professor_id)}); } /* GET PROFESSOR COUNT */ public int getProfessorCount() { database = prof_dbHelper.getReadableDatabase(); Cursor cursor = database.query(DB_ABSTRACTS.DBProfessor.DATABASE_TABLE, allColumns, null, null, null, null, null); int counter = cursor.getCount(); cursor.close(); return counter; } }