Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.darren.comp3074_assn2;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.content.Context;
- /**
- * Created by Darren on 2017-11-22.
- */
- public class DatabaseHelper extends SQLiteOpenHelper
- {
- //Logcat tag
- private static final String LOGT = "DatabaseHelper";
- //Database version
- private static final int DATABASE_VERSION = 1;
- //Database Name
- private static final String DATABASE_NAME = "HospitalDB";
- //Authentication Table
- private static final String TABLE_USER = "Users";
- //Table Names
- private static final String TABLE_PATIENTS = "Patients";
- private static final String TABLE_TESTS = "Tests";
- private static final String TABLE_NURSES = "Nurses";
- private static final String TABLE_DOCTORS = "Doctors";
- //Common column names
- private static final String KEY_ID = "ID";
- private static final String KEY_PATIENT_ID = "PatientID";
- private static final String KEY_FIRST_NAME = "FirstName";
- private static final String KEY_LAST_NAME = "LastName";
- private static final String KEY_DEPARTMENT = "Department";
- private static final String KEY_DOCTOR_ID = "DoctorID";
- //User table
- private static final String KEY_USERNAME = "Username";
- private static final String KEY_PASSWORD = "Password";
- private static final String KEY_ROLE = "Role";
- //Patient Table
- private static final String KEY_ROOM_NUM = "RoomNum";
- //Test Table
- private static final String KEY_TEST_ID = "TestID";
- private static final String KEY_BPL = "BPL";
- private static final String KEY_BPH = "BPH";
- private static final String KEY_TEMPERATURE = "TEMPERATURE";
- //Nurse Table
- private static final String KEY_NURSE_ID = "NurseID";
- //Table create statements
- //User table creation
- private static final String CREATE_TABLE_USER = "CREATE TABLE " + TABLE_USER + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
- + KEY_ROLE + " TEXT," + KEY_USERNAME + " TEXT," + KEY_PASSWORD + " TEXT,"
- + KEY_FIRST_NAME+ " TEXT," + KEY_LAST_NAME + " TEXT," + KEY_DEPARTMENT + " TEXT);";
- //Patient table creation
- private static final String CREATE_TABLE_PATIENTS = "CREATE TABLE" + TABLE_PATIENTS + "(" + KEY_PATIENT_ID + "INTEGER PRIMARY KEY," +
- KEY_FIRST_NAME + "TEXT," + KEY_LAST_NAME + "TEXT," + KEY_DEPARTMENT + "TEXT," + KEY_DOCTOR_ID + "INTEGER," + KEY_ROOM_NUM+ "INTEGER" + ");";
- //Tests table creation
- private static final String CREATE_TABLE_TESTS = "CREATE TABLE" + TABLE_TESTS + "(" + KEY_TEST_ID + "INTEGER PRIMARY KEY," + KEY_PATIENT_ID + "INTEGER,"+
- KEY_BPL + "INTEGER," + KEY_BPH + "INTEGER," + KEY_TEMPERATURE + "INTEGER" + ");";
- //Nurse table creation
- private static final String CREATE_TABLE_NURSES = "CREATE TABLE" + TABLE_NURSES + "(" + KEY_NURSE_ID + "INTEGER PRIMARY KEY," + KEY_FIRST_NAME + "TEXT," +
- KEY_LAST_NAME + "TEXT," + KEY_DEPARTMENT + "TEXT" + ");";
- //Doctor Table Creation
- private static final String CREATE_TABLE_DOCTORS = "CREATE TABLE" +TABLE_DOCTORS + "(" + KEY_DOCTOR_ID + "INTEGER PRIMARY KEY," + KEY_FIRST_NAME + "TEXT," +
- KEY_LAST_NAME + "TEXT," + KEY_DEPARTMENT + "TEXT" + ");";
- public DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- private long insertUser(SQLiteDatabase db, String Username, String Role, String Password, String FirstName, String LastName, String Department){
- ContentValues values = new ContentValues();
- values.put (KEY_USERNAME, Username);
- values.put (KEY_ROLE, Role);
- values.put (KEY_PASSWORD, Password);
- values.put (KEY_FIRST_NAME, FirstName);
- values.put (KEY_LAST_NAME, LastName);
- values.put (KEY_DEPARTMENT, Department);
- return db.insert(TABLE_USER, null, values);
- }
- public User login(String Username, String Password) {
- //Initialize userID
- Cursor c;
- User user = null;
- try {
- SQLiteDatabase db = this.getReadableDatabase();
- //Store the sql query for selecting the User Record
- String selectQuery =
- "SELECT * FROM " + TABLE_USER
- + " WHERE " + KEY_USERNAME + " = \"" + Username
- + "\" AND " + KEY_PASSWORD + " = \"" + Password + "\";";
- //Create a Cursor to store the Record and execute the Query stored in selectQuery
- c = db.rawQuery(selectQuery, null);
- if(c == null) {
- return null;
- }
- c.moveToFirst();
- //Set userID to the id of the User Record
- user = new User(
- c.getString(c.getColumnIndex(KEY_ID)),
- c.getString(c.getColumnIndex(KEY_USERNAME)),
- c.getString(c.getColumnIndex(KEY_FIRST_NAME)),
- c.getString(c.getColumnIndex(KEY_LAST_NAME)),
- c.getString(c.getColumnIndex(KEY_DEPARTMENT)),
- c.getString(c.getColumnIndex(KEY_ROLE))
- );
- return user;
- } catch(Exception e) {
- return null;
- }
- }
- public void dropTables(){
- SQLiteDatabase db = this.getReadableDatabase();
- db.execSQL("DROP TABLE IF EXISTS" + TABLE_USER);
- db.execSQL("DROP TABLE IF EXISTS" + TABLE_PATIENTS);
- db.execSQL("DROP TABLE IF EXISTS" + TABLE_TESTS);
- db.execSQL("DROP TABLE IF EXISTS" + TABLE_NURSES);
- db.execSQL("DROP TABLE IF EXISTS" + TABLE_DOCTORS);
- }
- @Override
- public void onCreate(SQLiteDatabase db){
- db.execSQL(CREATE_TABLE_USER);
- db.execSQL(CREATE_TABLE_PATIENTS);
- db.execSQL(CREATE_TABLE_TESTS);
- db.execSQL(CREATE_TABLE_NURSES);
- db.execSQL(CREATE_TABLE_DOCTORS);
- //Insert Users
- // Insert Doctor Demo User Record
- long doctorID = insertUser(db, "Doctor.Yeet", "doctor", "doctor", "Doc", "Doe", "Emergency");
- // Insert Nurse Demo User Record
- long nurseID = insertUser(db, "Nurse.Joy", "nurse", "nurse", "Nurse", "Doe", "Emergency");
- }
- //method for new versions of the app
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
- db.execSQL("DROP TABLE IF EXISTS" + TABLE_USER);
- db.execSQL("DROP TABLE IF EXISTS" + TABLE_PATIENTS);
- db.execSQL("DROP TABLE IF EXISTS" + TABLE_TESTS);
- db.execSQL("DROP TABLE IF EXISTS" + TABLE_NURSES);
- db.execSQL("DROP TABLE IF EXISTS" + TABLE_DOCTORS);
- //creates new tables
- onCreate(db);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement