Advertisement
Guest User

Untitled

a guest
Dec 5th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. package com.example.darren.comp3074_assn2;
  2.  
  3. import android.content.ContentValues;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. import android.content.Context;
  8. /**
  9. * Created by Darren on 2017-11-22.
  10. */
  11.  
  12. public class DatabaseHelper extends SQLiteOpenHelper
  13. {
  14. //Logcat tag
  15. private static final String LOGT = "DatabaseHelper";
  16. //Database version
  17. private static final int DATABASE_VERSION = 1;
  18. //Database Name
  19. private static final String DATABASE_NAME = "HospitalDB";
  20.  
  21. //Authentication Table
  22. private static final String TABLE_USER = "Users";
  23.  
  24. //Table Names
  25. private static final String TABLE_PATIENTS = "Patients";
  26. private static final String TABLE_TESTS = "Tests";
  27. private static final String TABLE_NURSES = "Nurses";
  28. private static final String TABLE_DOCTORS = "Doctors";
  29.  
  30. //Common column names
  31. private static final String KEY_ID = "ID";
  32. private static final String KEY_PATIENT_ID = "PatientID";
  33. private static final String KEY_FIRST_NAME = "FirstName";
  34. private static final String KEY_LAST_NAME = "LastName";
  35. private static final String KEY_DEPARTMENT = "Department";
  36. private static final String KEY_DOCTOR_ID = "DoctorID";
  37.  
  38. //User table
  39. private static final String KEY_USERNAME = "Username";
  40. private static final String KEY_PASSWORD = "Password";
  41. private static final String KEY_ROLE = "Role";
  42.  
  43. //Patient Table
  44. private static final String KEY_ROOM_NUM = "RoomNum";
  45.  
  46. //Test Table
  47. private static final String KEY_TEST_ID = "TestID";
  48. private static final String KEY_BPL = "BPL";
  49. private static final String KEY_BPH = "BPH";
  50. private static final String KEY_TEMPERATURE = "TEMPERATURE";
  51.  
  52. //Nurse Table
  53. private static final String KEY_NURSE_ID = "NurseID";
  54.  
  55. //Table create statements
  56. //User table creation
  57. private static final String CREATE_TABLE_USER = "CREATE TABLE " + TABLE_USER + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
  58. + KEY_ROLE + " TEXT," + KEY_USERNAME + " TEXT," + KEY_PASSWORD + " TEXT,"
  59. + KEY_FIRST_NAME+ " TEXT," + KEY_LAST_NAME + " TEXT," + KEY_DEPARTMENT + " TEXT);";
  60.  
  61. //Patient table creation
  62. private static final String CREATE_TABLE_PATIENTS = "CREATE TABLE" + TABLE_PATIENTS + "(" + KEY_PATIENT_ID + "INTEGER PRIMARY KEY," +
  63. KEY_FIRST_NAME + "TEXT," + KEY_LAST_NAME + "TEXT," + KEY_DEPARTMENT + "TEXT," + KEY_DOCTOR_ID + "INTEGER," + KEY_ROOM_NUM+ "INTEGER" + ");";
  64.  
  65. //Tests table creation
  66. private static final String CREATE_TABLE_TESTS = "CREATE TABLE" + TABLE_TESTS + "(" + KEY_TEST_ID + "INTEGER PRIMARY KEY," + KEY_PATIENT_ID + "INTEGER,"+
  67. KEY_BPL + "INTEGER," + KEY_BPH + "INTEGER," + KEY_TEMPERATURE + "INTEGER" + ");";
  68.  
  69. //Nurse table creation
  70. private static final String CREATE_TABLE_NURSES = "CREATE TABLE" + TABLE_NURSES + "(" + KEY_NURSE_ID + "INTEGER PRIMARY KEY," + KEY_FIRST_NAME + "TEXT," +
  71. KEY_LAST_NAME + "TEXT," + KEY_DEPARTMENT + "TEXT" + ");";
  72.  
  73. //Doctor Table Creation
  74. private static final String CREATE_TABLE_DOCTORS = "CREATE TABLE" +TABLE_DOCTORS + "(" + KEY_DOCTOR_ID + "INTEGER PRIMARY KEY," + KEY_FIRST_NAME + "TEXT," +
  75. KEY_LAST_NAME + "TEXT," + KEY_DEPARTMENT + "TEXT" + ");";
  76.  
  77. public DatabaseHelper(Context context) {
  78. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  79. }
  80.  
  81.  
  82. private long insertUser(SQLiteDatabase db, String Username, String Role, String Password, String FirstName, String LastName, String Department){
  83.  
  84. ContentValues values = new ContentValues();
  85. values.put (KEY_USERNAME, Username);
  86. values.put (KEY_ROLE, Role);
  87. values.put (KEY_PASSWORD, Password);
  88. values.put (KEY_FIRST_NAME, FirstName);
  89. values.put (KEY_LAST_NAME, LastName);
  90. values.put (KEY_DEPARTMENT, Department);
  91. return db.insert(TABLE_USER, null, values);
  92.  
  93. }
  94.  
  95. public User login(String Username, String Password) {
  96. //Initialize userID
  97. Cursor c;
  98. User user = null;
  99.  
  100. try {
  101.  
  102. SQLiteDatabase db = this.getReadableDatabase();
  103.  
  104. //Store the sql query for selecting the User Record
  105. String selectQuery =
  106. "SELECT * FROM " + TABLE_USER
  107. + " WHERE " + KEY_USERNAME + " = \"" + Username
  108. + "\" AND " + KEY_PASSWORD + " = \"" + Password + "\";";
  109.  
  110. //Create a Cursor to store the Record and execute the Query stored in selectQuery
  111. c = db.rawQuery(selectQuery, null);
  112.  
  113.  
  114. if(c == null) {
  115. return null;
  116. }
  117.  
  118. c.moveToFirst();
  119.  
  120. //Set userID to the id of the User Record
  121. user = new User(
  122. c.getString(c.getColumnIndex(KEY_ID)),
  123. c.getString(c.getColumnIndex(KEY_USERNAME)),
  124. c.getString(c.getColumnIndex(KEY_FIRST_NAME)),
  125. c.getString(c.getColumnIndex(KEY_LAST_NAME)),
  126. c.getString(c.getColumnIndex(KEY_DEPARTMENT)),
  127. c.getString(c.getColumnIndex(KEY_ROLE))
  128. );
  129.  
  130. return user;
  131. } catch(Exception e) {
  132. return null;
  133. }
  134.  
  135. }
  136.  
  137. public void dropTables(){
  138.  
  139. SQLiteDatabase db = this.getReadableDatabase();
  140. db.execSQL("DROP TABLE IF EXISTS" + TABLE_USER);
  141. db.execSQL("DROP TABLE IF EXISTS" + TABLE_PATIENTS);
  142. db.execSQL("DROP TABLE IF EXISTS" + TABLE_TESTS);
  143. db.execSQL("DROP TABLE IF EXISTS" + TABLE_NURSES);
  144. db.execSQL("DROP TABLE IF EXISTS" + TABLE_DOCTORS);
  145. }
  146.  
  147. @Override
  148. public void onCreate(SQLiteDatabase db){
  149. db.execSQL(CREATE_TABLE_USER);
  150. db.execSQL(CREATE_TABLE_PATIENTS);
  151. db.execSQL(CREATE_TABLE_TESTS);
  152. db.execSQL(CREATE_TABLE_NURSES);
  153. db.execSQL(CREATE_TABLE_DOCTORS);
  154.  
  155.  
  156. //Insert Users
  157. // Insert Doctor Demo User Record
  158. long doctorID = insertUser(db, "Doctor.Yeet", "doctor", "doctor", "Doc", "Doe", "Emergency");
  159. // Insert Nurse Demo User Record
  160. long nurseID = insertUser(db, "Nurse.Joy", "nurse", "nurse", "Nurse", "Doe", "Emergency");
  161. }
  162.  
  163. //method for new versions of the app
  164. @Override
  165. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
  166. db.execSQL("DROP TABLE IF EXISTS" + TABLE_USER);
  167. db.execSQL("DROP TABLE IF EXISTS" + TABLE_PATIENTS);
  168. db.execSQL("DROP TABLE IF EXISTS" + TABLE_TESTS);
  169. db.execSQL("DROP TABLE IF EXISTS" + TABLE_NURSES);
  170. db.execSQL("DROP TABLE IF EXISTS" + TABLE_DOCTORS);
  171.  
  172. //creates new tables
  173. onCreate(db);
  174. }
  175.  
  176.  
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement