| Talking Reminder this project is intenfed for people who are not tech savvy

ReminderDatabase.java
1 package com.sudogeeks.talking_reminder;
2
3 import android.content.ContentValues;
4 import android.content.Context;
5 import android.database.Cursor;
6 import android.database.sqlite.SQLiteDatabase;
7 import android.database.sqlite.SQLiteOpenHelper;
8
9 import java.util.ArrayList;
10 import java.util.List;
11
15 public class ReminderDatabase extends SQLiteOpenHelper {
16 // Database Version
17 private static final int DATABASE_VERSION = 1;
18
19 // Database Name
20 private static final String DATABASE_NAME = "ReminderDatabase";
21
22 // Table name
23 private static final String TABLE_REMINDERS = "ReminderTable";
24
25 // Table Columns names
26 private static final String KEY_ID = "id";
27 private static final String KEY_TITLE = "title";
28 private static final String KEY_DATE = "date";
29 private static final String KEY_TIME = "time";
30 private static final String KEY_REPEAT = "repeat";
31 private static final String KEY_REPEAT_NO = "repeat_no";
32 private static final String KEY_REPEAT_TYPE = "repeat_type";
33 private static final String KEY_ACTIVE = "active";
34
35 public ReminderDatabase(Context context) {
36 super(context, DATABASE_NAME, null, DATABASE_VERSION);
37 }
38
44 @Override
45 public void onCreate(SQLiteDatabase db) {
46 String CREATE_REMINDERS_TABLE = "CREATE TABLE " + TABLE_REMINDERS +
47 "("
48 + KEY_ID + " INTEGER PRIMARY KEY,"
49 + KEY_TITLE + " TEXT,"
50 + KEY_DATE + " TEXT,"
51 + KEY_TIME + " INTEGER,"
52 + KEY_REPEAT + " BOOLEAN,"
53 + KEY_REPEAT_NO + " INTEGER,"
54 + KEY_REPEAT_TYPE + " TEXT,"
55 + KEY_ACTIVE + " BOOLEAN" + ")";
56 db.execSQL(CREATE_REMINDERS_TABLE);
57 }
58
66 @Override
67 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
68 // Drop older table if existed
69 if (oldVersion >= newVersion)
70 return;
71 db.execSQL("DROP TABLE IF EXISTS " + TABLE_REMINDERS);
72
73 // Create tables again
74 onCreate(db);
75 }
76
83 public int addReminder(ReminderDO reminderDO) {
84 SQLiteDatabase db = this.getWritableDatabase();
85 ContentValues values = new ContentValues();
86
87 values.put(KEY_TITLE, reminderDO.getTitle());
88 values.put(KEY_DATE, reminderDO.getDate());
89 values.put(KEY_TIME, reminderDO.getTime());
90 values.put(KEY_REPEAT, reminderDO.getRepeat());
91 values.put(KEY_REPEAT_NO, reminderDO.getRepeatNo());
92 values.put(KEY_REPEAT_TYPE, reminderDO.getRepeatType());
93 values.put(KEY_ACTIVE, reminderDO.getActive());
94
95 // Inserting Row
96 long ID = db.insert(TABLE_REMINDERS, null, values);
97 db.close();
98 return (int) ID;
99 }
100
106 public ReminderDO getReminder(int id) {
107 SQLiteDatabase db = this.getReadableDatabase();
108
109 Cursor cursor = db.query(TABLE_REMINDERS, new String[]
110 {
111 KEY_ID,
112 KEY_TITLE,
113 KEY_DATE,
114 KEY_TIME,
115 KEY_REPEAT,
116 KEY_REPEAT_NO,
117 KEY_REPEAT_TYPE,
118 KEY_ACTIVE
119 }, KEY_ID + "=?",
120
121 new String[]{String.valueOf(id)}, null, null, null, null);
122
123 if (cursor != null)
124 cursor.moveToFirst();
125
126 ReminderDO reminderDO = new ReminderDO(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
127 cursor.getString(2), cursor.getString(3), cursor.getString(4),
128 cursor.getString(5), cursor.getString(6), cursor.getString(7));
129
130 return reminderDO;
131 }
132
137 // Getting all Reminders
138 public List<ReminderDO> getAllReminders() {
139 List<ReminderDO> reminderDOList = new ArrayList<>();
140
141 // Select all Query
142 String selectQuery = "SELECT * FROM " + TABLE_REMINDERS;
143
144 SQLiteDatabase db = this.getWritableDatabase();
145 Cursor cursor = db.rawQuery(selectQuery, null);
146
147 // Looping through all rows and adding to list
148 if (cursor.moveToFirst()) {
149 do {
150 ReminderDO reminderDO = new ReminderDO();
151 reminderDO.setID(Integer.parseInt(cursor.getString(0)));
152 reminderDO.setTitle(cursor.getString(1));
153 reminderDO.setDate(cursor.getString(2));
154 reminderDO.setTime(cursor.getString(3));
155 reminderDO.setRepeat(cursor.getString(4));
156 reminderDO.setRepeatNo(cursor.getString(5));
157 reminderDO.setRepeatType(cursor.getString(6));
158 reminderDO.setActive(cursor.getString(7));
159
160 // Adding Reminders to list
161 reminderDOList.add(reminderDO);
162 } while (cursor.moveToNext());
163 }
164 return reminderDOList;
165 }
166
172 public int getRemindersCount() {
173 String countQuery = "SELECT * FROM " + TABLE_REMINDERS;
174 SQLiteDatabase db = this.getReadableDatabase();
175 Cursor cursor = db.rawQuery(countQuery, null);
176 cursor.close();
177
178 return cursor.getCount();
179 }
180
187 public int updateReminder(ReminderDO reminderDO) {
188 SQLiteDatabase db = this.getWritableDatabase();
189 ContentValues values = new ContentValues();
190 values.put(KEY_TITLE, reminderDO.getTitle());
191 values.put(KEY_DATE, reminderDO.getDate());
192 values.put(KEY_TIME, reminderDO.getTime());
193 values.put(KEY_REPEAT, reminderDO.getRepeat());
194 values.put(KEY_REPEAT_NO, reminderDO.getRepeatNo());
195 values.put(KEY_REPEAT_TYPE, reminderDO.getRepeatType());
196 values.put(KEY_ACTIVE, reminderDO.getActive());
197
198 // Updating row
199 return db.update(TABLE_REMINDERS, values, KEY_ID + "=?",
200 new String[]{String.valueOf(reminderDO.getID())});
201 }
202
208 public void deleteReminder(ReminderDO reminderDO) {
209 SQLiteDatabase db = this.getWritableDatabase();
210 db.delete(TABLE_REMINDERS, KEY_ID + "=?",
211 new String[]{String.valueOf(reminderDO.getID())});
212 db.close();
213 }
214 }
com.sudogeeks.talking_reminder.ReminderDatabase.deleteReminder
void deleteReminder(ReminderDO reminderDO)
Definition: ReminderDatabase.java:208
com.sudogeeks.talking_reminder.ReminderDatabase.getReminder
ReminderDO getReminder(int id)
Definition: ReminderDatabase.java:106
com.sudogeeks.talking_reminder.ReminderDatabase.updateReminder
int updateReminder(ReminderDO reminderDO)
Definition: ReminderDatabase.java:187
com.sudogeeks.talking_reminder.ReminderDatabase.getAllReminders
List< ReminderDO > getAllReminders()
Definition: ReminderDatabase.java:138
com.sudogeeks.talking_reminder.ReminderDO
Data Object class for Reminder This class conatins all the attributes of reminder.
Definition: ReminderDO.java:15
com.sudogeeks.talking_reminder.ReminderDatabase.getRemindersCount
int getRemindersCount()
Definition: ReminderDatabase.java:172
com.sudogeeks.talking_reminder.ReminderDatabase
Class to manage database This class is for managing the database.
Definition: ReminderDatabase.java:15
com.sudogeeks.talking_reminder.ReminderDatabase.onUpgrade
void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
Definition: ReminderDatabase.java:67
com.sudogeeks.talking_reminder.ReminderDatabase.onCreate
void onCreate(SQLiteDatabase db)
Definition: ReminderDatabase.java:45
com.sudogeeks.talking_reminder.ReminderDatabase.addReminder
int addReminder(ReminderDO reminderDO)
Definition: ReminderDatabase.java:83