Android Open Source - ProjectStudio Professor Data Source






From Project

Back to project page ProjectStudio.

License

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.

Java Source Code

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;
    }
}




Java Source Code List

DB_Provider.CourseDataSource.java
DB_Provider.DBManager.java
DB_Provider.DB_ABSTRACTS.java
DB_Provider.ProfessorDataSource.java
DB_Provider.TaskDataSource.java
Drawables.DrawableAlignedButton.java
FontPackage.TypefaceSpan.java
adapters.CourseCardAdapter.java
adapters.ExamsPassedCardAdapter.java
adapters.ExamsPendingCardAdapter.java
adapters.Exams_TabsPagerAdapter.java
adapters.HomeCardAdapter.java
adapters.MatesCardAdapter.java
adapters.NavDrawerListAdapter.java
adapters.ProfessorCardAdapter.java
adapters.TaskCardAdapter.java
adapters.TasksTabPagerAdapter.java
com.example.uniutilproject.BuildConfig.java
com.example.uniutilproject.UniUtil_MainActivity.java
days_fragments.FridayTaskFragment.java
days_fragments.MondayTaskFragment.java
days_fragments.SaturdayTaskFragment.java
days_fragments.SundayTaskFragment.java
days_fragments.ThursdayTaskFragment.java
days_fragments.TuesdayTaskFragment.java
days_fragments.WednesdayTaskFragment.java
dialog_fragments.DatePickerDialogFragment.java
dialog_fragments.MatesDialogFragment.java
dialog_fragments.ProfessorsDialogFragment.java
dialog_fragments.TimePickerDialogFragment.java
fragments.AddCourseFragment.java
fragments.AddProfFragment.java
fragments.AddTaskFragment.java
fragments.CoursesFragment.java
fragments.CursorLoaderListFragment.java
fragments.ExamsFragment.java
fragments.ExamsPassedFragment.java
fragments.ExamsPendingFragment.java
fragments.HomeFragment.java
fragments.MatesFragment.java
fragments.ProfessorsFragment.java
fragments.StudyPlanFragment.java
fragments.TasksFragment.java
fragments.UpdateCourseFragment.java
fragments.UpdateProfessorFragment.java
interfaces.NoticeDialogListener.java
interfaces.package-info.java
model.Course.java
model.Days.java
model.MyParcelable.java
model.NavDrawerItem.java
model.Professor.java
model.Task.java