Advertisement
Guest User

Untitled

a guest
Nov 29th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.19 KB | None | 0 0
  1. package com.pothier.eric.marc.splitty;
  2.  
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7.  
  8. import java.util.ArrayList;
  9.  
  10. /**
  11. * Created by etiennelaprise on 2017-11-22.
  12. */
  13.  
  14. public class DatabaseManager extends SQLiteOpenHelper {
  15. private static final String DATABASE_NAME = "splittyDB";
  16. private static final int DATABASE_VERSION = 1;
  17.  
  18. // Person Table
  19. private static final String TABLE_PARTICIPANT = "participant";
  20. private static final String PARTICIPANT_ID = "participant_id";
  21. private static final String FIRST_NAME = "first_name";
  22. private static final String LAST_NAME = "last_name";
  23. private static final String PHONE = "phone";
  24. private static final String EMAIL = "email";
  25. private static final String USERNAME = "username";
  26. private static final String PASSWORD = "password";
  27.  
  28. // Event Table
  29. private static final String TABLE_EVENT = "event";
  30. private static final String EVENT_ID = "event_id";
  31. private static final String EVENT_NAME = "event_name";
  32. private static final String EVENT_ACTIVE = "event_active";
  33.  
  34. // Event_Participant Table
  35. private static final String TABLE_EVENT_PARTICIPANT = "event_participant";
  36. private static final String AMOUNT_PAID = "amount_paid";
  37. private static final String FK_EVENT_ID = "fk_event_id";
  38. private static final String FK_PARTICIPANT_ID = "fk_participant_id";
  39.  
  40. public DatabaseManager(Context context) {
  41. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  42. }
  43.  
  44. // SQL create table
  45. public void onCreate(SQLiteDatabase db) {
  46. // Create Participant Table
  47. String sqlCreateParticipant = "create table "
  48. + TABLE_PARTICIPANT + "("
  49. + PARTICIPANT_ID + " integer primary key autoincrement, "
  50. + FIRST_NAME + " text, "
  51. + LAST_NAME + " text, "
  52. + PHONE + " text, "
  53. + EMAIL + " text, "
  54. + USERNAME + " text, "
  55. + PASSWORD + " text)";
  56.  
  57. // Create Event Table
  58. String sqlCreateEvent = "create table "
  59. + TABLE_EVENT + "("
  60. + EVENT_ID + " integer primary key autoincrement, "
  61. + EVENT_NAME + " text, "
  62. + EVENT_ACTIVE + " boolean)";
  63.  
  64. // Create Event_Participant Table
  65. String sqlCreateEvent_Participant = "create table "
  66. + TABLE_EVENT_PARTICIPANT + "("
  67. + FK_EVENT_ID + " integer,"
  68. + " FOREIGN KEY (" + FK_EVENT_ID + ") REFERENCES " + TABLE_EVENT + "(" + EVENT_ID +")"
  69. + FK_PARTICIPANT_ID + " integer,"
  70. + " FOREIGN KEY (" + FK_PARTICIPANT_ID + ") REFERENCES " + TABLE_PARTICIPANT + "(" + PARTICIPANT_ID +")"
  71. + AMOUNT_PAID + " double,"
  72. + ");";
  73.  
  74. db.execSQL(sqlCreateParticipant);
  75. db.execSQL(sqlCreateEvent);
  76. db.execSQL(sqlCreateEvent_Participant);
  77. }
  78.  
  79. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  80. db.execSQL("drop table id exists " + TABLE_PARTICIPANT);
  81. db.execSQL("drop table id exists " + TABLE_EVENT);
  82. db.execSQL("drop table id exists " + TABLE_EVENT_PARTICIPANT);
  83.  
  84. onCreate(db);
  85. }
  86.  
  87. /// Insert Statement ///
  88. // Add new participant record to Participant Table
  89. public void insertParticipant(Participant participant) {
  90. SQLiteDatabase db = this.getWritableDatabase();
  91. String sqlInsert = "insert into " + TABLE_PARTICIPANT
  92. + " values(null, '"
  93. + participant.getFirstName() + "', '"
  94. + participant.getLastName() + "', '"
  95. + participant.getPhone() + "', '"
  96. + participant.getEmail() + "', '"
  97. + participant.getUserName() + "', '"
  98. + participant.getPassword() + "')";
  99.  
  100. db.execSQL(sqlInsert);
  101. db.close();
  102. }
  103. // Add new event record to Event Table
  104. public void insertEvent(Event event) {
  105. SQLiteDatabase db = this.getWritableDatabase();
  106. String sqlInsert = "insert into " + TABLE_EVENT
  107. + " values(null, '"
  108. + event.getEventName() + "', '"
  109. + event.getEventActive() + "')";
  110.  
  111. db.execSQL(sqlInsert);
  112. db.close();
  113. }
  114. // Add new event_participant record to Event_Participant Table
  115. public void insertEvent_Participant(Event_Participant event_participant) {
  116. SQLiteDatabase db = this.getWritableDatabase();
  117. String sqlInsert = "insert into " + TABLE_EVENT_PARTICIPANT
  118. + " values("
  119. + event_participant.getEvent_id() + "', '"
  120. + event_participant.getParticipant_id() + "', '"
  121. + event_participant.getAmountPaid() + "')";
  122.  
  123. db.execSQL(sqlInsert);
  124. db.close();
  125. }
  126.  
  127. /// Select using ID ///
  128. //Select a participant using ID
  129. public Participant selectParticipantById(int id) {
  130. SQLiteDatabase db = this.getWritableDatabase();
  131. String sqlQuery = "select * from " + TABLE_PARTICIPANT + " where " + PARTICIPANT_ID + " = "
  132. + id;
  133. Cursor cursor = db.rawQuery(sqlQuery, null);
  134. Participant participant = null;
  135. if (cursor.moveToFirst())
  136. participant = new Participant(
  137. Integer.parseInt(cursor.getString(0)),
  138. cursor.getString(1),
  139. cursor.getString(2),
  140. cursor.getString(3),
  141. cursor.getString(4));
  142. return participant;
  143. }
  144. public Event selectEventById(int id) {
  145. SQLiteDatabase db = this.getWritableDatabase();
  146. String sqlQuery = "select * from " + TABLE_EVENT + " where " + EVENT_ID + " = " + id;
  147. Cursor cursor = db.rawQuery(sqlQuery, null);
  148. Event event = null;
  149. if (cursor.moveToFirst())
  150. event = new Event(
  151. Integer.parseInt(cursor.getString(0)),
  152. cursor.getString(1),
  153. Boolean.parseBoolean(cursor.getString(2)));
  154. return event;
  155. }
  156. public Event_Participant selectEventParticipantById(int eventID, int participantID) {
  157. SQLiteDatabase db = this.getWritableDatabase();
  158. String sqlQuery = "select * from " + TABLE_EVENT_PARTICIPANT + " where " + FK_EVENT_ID
  159. + " = " + eventID + " and " + FK_PARTICIPANT_ID + " = " + participantID;
  160. Cursor cursor = db.rawQuery(sqlQuery, null);
  161. Event_Participant event_participant = null;
  162. if (cursor.moveToFirst())
  163. event_participant = new Event_Participant(
  164. Integer.parseInt(cursor.getString(0)),
  165. Integer.parseInt(cursor.getString(1)),
  166. Double.parseDouble(cursor.getString(2)));
  167. return event_participant;
  168. }
  169.  
  170. /// Select All Statements //
  171. // Select all participants
  172. public ArrayList<Participant> selectAllParticipant(){
  173. SQLiteDatabase db = this.getWritableDatabase();
  174.  
  175. String sqlQuery = "select * from " + TABLE_PARTICIPANT;
  176.  
  177. Cursor cursor = db.rawQuery(sqlQuery, null);
  178.  
  179. ArrayList<Participant> participants = new ArrayList<>();
  180. while (cursor.moveToNext()){
  181. Participant currentParticipant = new Participant(
  182. Integer.parseInt(cursor.getString(0)),
  183. cursor.getString(1),
  184. cursor.getString(2),
  185. cursor.getString(3),
  186. cursor.getString(4));
  187. participants.add(currentParticipant);
  188. }
  189. db.close();
  190. return participants;
  191. }
  192. // Select all events
  193. public ArrayList<Event> selectAllEvent(){
  194. SQLiteDatabase db = this.getWritableDatabase();
  195.  
  196. String sqlQuery = "select * from " + TABLE_EVENT;
  197.  
  198. Cursor cursor = db.rawQuery(sqlQuery, null);
  199.  
  200. ArrayList<Event> events = new ArrayList<>();
  201. while (cursor.moveToNext()){
  202. Event currentEvent = new Event(
  203. Integer.parseInt(cursor.getString(0)),
  204. cursor.getString(1),
  205. Boolean.parseBoolean(cursor.getString(2)));
  206. events.add(currentEvent);
  207. }
  208. db.close();
  209. return events;
  210. }
  211. // Select all event_participants
  212. public ArrayList<Event_Participant> selectAllEventParticipant(){
  213. SQLiteDatabase db = this.getWritableDatabase();
  214.  
  215. String sqlQuery = "select * from " + TABLE_EVENT_PARTICIPANT;
  216.  
  217. Cursor cursor = db.rawQuery(sqlQuery, null);
  218.  
  219. ArrayList<Event_Participant> event_participants = new ArrayList<>();
  220. while (cursor.moveToNext()){
  221. Event_Participant currentEventParticipant = new Event_Participant(
  222. Integer.parseInt(cursor.getString(0)),
  223. Integer.parseInt(cursor.getString(1)),
  224. Double.parseDouble(cursor.getString(2)));
  225. event_participants.add(currentEventParticipant);
  226. }
  227. db.close();
  228. return event_participants;
  229. }
  230.  
  231. /// Delete Statements ///
  232. // Delete participant record using ID
  233. public void deleteParticipantById(int id) {
  234. SQLiteDatabase db = this.getWritableDatabase();
  235. String sqlDelete = "delete from " + TABLE_PARTICIPANT + " where " + PARTICIPANT_ID + " = " + id;
  236.  
  237. db.execSQL(sqlDelete);
  238. db.close();
  239. }
  240. // Delete event record using ID
  241. public void deleteEventById(int id) {
  242. SQLiteDatabase db = this.getWritableDatabase();
  243. String sqlDelete = "delete from " + TABLE_EVENT + " where " + EVENT_ID + " = " + id;
  244.  
  245. db.execSQL(sqlDelete);
  246. db.close();
  247. }
  248. // Delete event_participant record using ID
  249. public void deleteEventParticipantById(int eventID, int participantID) {
  250. SQLiteDatabase db = this.getWritableDatabase();
  251. String sqlDelete = "delete from " + TABLE_EVENT_PARTICIPANT + " where "
  252. + FK_EVENT_ID + " = " + eventID + " and " + FK_PARTICIPANT_ID + " = " + participantID;
  253.  
  254. db.execSQL(sqlDelete);
  255. db.close();
  256. }
  257.  
  258. /// Update Statements ///
  259. //Edit participant record using ID
  260. public void updateParticipantById(int id, String firstName, String lastName, String phone, String email) {
  261. SQLiteDatabase db = this.getWritableDatabase();
  262. String sqlUpdate = "update " + TABLE_PARTICIPANT + " set " + FIRST_NAME + " = '" + firstName
  263. + "', " + LAST_NAME + " = '" + lastName + "', " + EMAIL + " = '" + email + "' where "
  264. + PARTICIPANT_ID + " = " + id;
  265.  
  266. db.execSQL(sqlUpdate);
  267. db.close();
  268. }
  269. //Edit participant record using ID
  270. public void updateEventById(int id, String eventName, boolean eventActive) {
  271. SQLiteDatabase db = this.getWritableDatabase();
  272. String sqlUpdate = "update " + TABLE_EVENT + " set " + EVENT_NAME + " = '" + eventName
  273. + "', " + EVENT_ACTIVE + " = '" + eventActive + "' where " + EVENT_ID + " = " + id;
  274.  
  275. db.execSQL(sqlUpdate);
  276. db.close();
  277. }
  278. //Edit event_participant record using ID
  279. public void updateEventParticipantById(int eventID, int participantID, double amountPaid) {
  280. SQLiteDatabase db = this.getWritableDatabase();
  281. String sqlUpdate = "update " + TABLE_EVENT_PARTICIPANT + " set " + AMOUNT_PAID + " = '"
  282. + amountPaid + "' where " + FK_EVENT_ID + " = " + eventID + " and "
  283. + FK_PARTICIPANT_ID + " = " + participantID;
  284.  
  285. db.execSQL(sqlUpdate);
  286. db.close();
  287. }
  288.  
  289. public boolean loginValidation(String usernamre, String password){
  290. SQLiteDatabase db = this.getWritableDatabase();
  291.  
  292. String sqlQuery = "select " + USERNAME + ", " + PASSWORD + " from " + TABLE_PARTICIPANT
  293. + " WHERE " + USERNAME + " = " + usernamre + " AND " + PASSWORD + " = " + password;
  294.  
  295. Cursor cursor = db.rawQuery(sqlQuery, null);
  296.  
  297. if(cursor!=null)
  298. return true;
  299. else
  300. return false;
  301. }
  302.  
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement