· 4 years ago · May 06, 2021, 08:42 PM
1package com.iomt.android;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8
9import java.util.ArrayList;
10import java.util.HashMap;
11import java.util.List;
12import java.util.Map;
13
14public class DatabaseHelper extends SQLiteOpenHelper {
15
16 // Database Version
17 private static final int DATABASE_VERSION = 1;
18
19 // Database Name
20 private static final String DATABASE_NAME = "notes_db";
21
22
23 public DatabaseHelper(Context context) {
24 super(context, DATABASE_NAME, null, DATABASE_VERSION);
25 }
26
27 // Creating Tables
28 @Override
29 public void onCreate(SQLiteDatabase db) {
30
31 // create notes table
32 db.execSQL(Note.CREATE_TABLE);
33 }
34
35 // Upgrading database
36 @Override
37 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
38 // Drop older table if existed
39 db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
40
41 // Create tables again
42 onCreate(db);
43 }
44
45 public long insertNote(Map<String, String> data) {
46 // get writable database as we want to write data
47 SQLiteDatabase db = this.getWritableDatabase();
48
49 ContentValues values = new ContentValues();
50 // `id` and `timestamp` will be inserted automatically.
51 // no need to add them
52 try {
53 values.put(Note.COLUMN_HEART_RATE, data.get(Note.COLUMN_HEART_RATE));
54 values.put(Note.COLUMN_RESP_RATE, data.get(Note.COLUMN_RESP_RATE));
55 values.put(Note.COLUMN_INSP, data.get(Note.COLUMN_INSP));
56 values.put(Note.COLUMN_EXP, data.get(Note.COLUMN_EXP));
57 values.put(Note.COLUMN_CADENCE, data.get(Note.COLUMN_CADENCE));
58 values.put(Note.COLUMN_STEP_COUNT, data.get(Note.COLUMN_STEP_COUNT));
59 values.put(Note.COLUMN_ACT, data.get(Note.COLUMN_ACT));
60 values.put(Note.COLUMN_CLI, data.get(Note.COLUMN_CLI));
61 } catch (Exception ex) {
62
63 }
64
65 // insert row
66 long id = db.insert(Note.TABLE_NAME, null, values);
67
68 // close db connection
69 db.close();
70
71 // return newly inserted row id
72 return id;
73 }
74
75 public Note getNote(long id) {
76 // get readable database as we are not inserting anything
77 SQLiteDatabase db = this.getReadableDatabase();
78
79 Cursor cursor = db.query(Note.TABLE_NAME,
80 new String[]{
81 Note.COLUMN_ID,
82 Note.COLUMN_HEART_RATE,
83 Note.COLUMN_RESP_RATE,
84 Note.COLUMN_INSP,
85 Note.COLUMN_EXP,
86 Note.COLUMN_CADENCE,
87 Note.COLUMN_STEP_COUNT,
88 Note.COLUMN_ACT,
89 Note.COLUMN_CLI,
90 Note.COLUMN_TIMESTAMP
91 },
92 Note.COLUMN_ID + "=?",
93 new String[]{String.valueOf(id)}, null, null, null, null);
94
95 if (cursor != null)
96 cursor.moveToFirst();
97
98 // prepare note object
99 Map<String, String> result = new HashMap<>();
100 try {
101 assert cursor != null;
102 result.put("HeartRate", cursor.getString(cursor.getColumnIndex(Note.COLUMN_HEART_RATE)));
103 result.put("RespRate", cursor.getString(cursor.getColumnIndex(Note.COLUMN_RESP_RATE)));
104 result.put("Insp", cursor.getString(cursor.getColumnIndex(Note.COLUMN_INSP)));
105 result.put("Exp", cursor.getString(cursor.getColumnIndex(Note.COLUMN_EXP)));
106 result.put("Cadence", cursor.getString(cursor.getColumnIndex(Note.COLUMN_CADENCE)));
107 result.put("Steps", cursor.getString(cursor.getColumnIndex(Note.COLUMN_STEP_COUNT)));
108 result.put("Activity", cursor.getString(cursor.getColumnIndex(Note.COLUMN_ACT)));
109 result.put("Clitime", cursor.getString(cursor.getColumnIndex(Note.COLUMN_CLI)));
110 } catch (Exception ex) {
111
112 }
113 Note note = new Note(
114 cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)),
115 result,
116 cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
117 // close the db connection
118 cursor.close();
119
120 return note;
121 }
122
123 /*public List<Note> getAllNotes() {
124 List<Note> notes = new ArrayList<>();
125
126 // Select All Query
127 String selectQuery = "SELECT * FROM " + Note.TABLE_NAME + " ORDER BY " +
128 Note.COLUMN_TIMESTAMP + " DESC";
129
130 SQLiteDatabase db = this.getWritableDatabase();
131 Cursor cursor = db.rawQuery(selectQuery, null);
132
133 // looping through all rows and adding to list
134 if (cursor.moveToFirst()) {
135 do {
136 Note note = new Note();
137 note.setId(cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)));
138 note.setNote(cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)));
139 note.setTimestamp(cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
140
141 notes.add(note);
142 } while (cursor.moveToNext());
143 }
144
145 // close db connection
146 db.close();
147
148 // return notes list
149 return notes;
150 }*/
151
152 /*public int getNotesCount() {
153 String countQuery = "SELECT * FROM " + Note.TABLE_NAME;
154 SQLiteDatabase db = this.getReadableDatabase();
155 Cursor cursor = db.rawQuery(countQuery, null);
156
157 int count = cursor.getCount();
158 cursor.close();
159
160 return count;
161 }
162
163 public int updateNote(Note note) {
164 SQLiteDatabase db = this.getWritableDatabase();
165
166 ContentValues values = new ContentValues();
167 values.put(Note.COLUMN_NOTE, note.getNote());
168
169 // updating row
170 return db.update(Note.TABLE_NAME, values, Note.COLUMN_ID + " = ?",
171 new String[]{String.valueOf(note.getId())});
172 }*/
173
174 public void deleteNote(Note note) {
175 SQLiteDatabase db = this.getWritableDatabase();
176 db.delete(Note.TABLE_NAME, Note.COLUMN_ID + " = ?",
177 new String[]{String.valueOf(note.getId())});
178 db.close();
179 }
180
181 public void clear() {
182 List<Note> notes = new ArrayList<>();
183
184 // Select All Query
185 String selectQuery = "SELECT * FROM " + Note.TABLE_NAME + " ORDER BY " +
186 Note.COLUMN_TIMESTAMP + " DESC";
187
188 SQLiteDatabase db = this.getWritableDatabase();
189 Cursor cursor = db.rawQuery(selectQuery, null);
190
191 // looping through all rows and adding to list
192 if (cursor.moveToFirst()) {
193 do {
194 Note note = new Note();
195 note.setId(cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)));
196
197 Map<String, String> result = new HashMap<>();
198 try {
199 result.put("HeartRate", cursor.getString(cursor.getColumnIndex(Note.COLUMN_HEART_RATE)));
200 result.put("RespRate", cursor.getString(cursor.getColumnIndex(Note.COLUMN_RESP_RATE)));
201 result.put("Insp", cursor.getString(cursor.getColumnIndex(Note.COLUMN_INSP)));
202 result.put("Exp", cursor.getString(cursor.getColumnIndex(Note.COLUMN_EXP)));
203 result.put("Cadence", cursor.getString(cursor.getColumnIndex(Note.COLUMN_CADENCE)));
204 result.put("Steps", cursor.getString(cursor.getColumnIndex(Note.COLUMN_STEP_COUNT)));
205 result.put("Activity", cursor.getString(cursor.getColumnIndex(Note.COLUMN_ACT)));
206 result.put("Clitime", cursor.getString(cursor.getColumnIndex(Note.COLUMN_CLI)));
207 } catch (Exception ex) {
208
209 }
210 note.setNote(result);
211 note.setTimestamp(cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
212
213 db.delete(Note.TABLE_NAME, Note.COLUMN_ID + " = ?",
214 new String[]{String.valueOf(note.getId())});
215 } while (cursor.moveToNext());
216 }
217
218 // close db connection
219 db.close();
220 }
221}