· 9 years ago · Dec 01, 2016, 12:40 AM
1package com.example.hashdog.ditislamicsocietyapp;
2
3import android.content.ContentValues;
4 import android.content.Context;
5 import android.database.Cursor;
6 import android.database.SQLException;
7 import android.database.sqlite.SQLiteDatabase;
8 import android.database.sqlite.SQLiteOpenHelper;
9 import android.util.Log;
10
11import static android.R.id.primary;
12
13public class DatabaseManager
14{
15 // These are the names of the columns the table will contain
16 public static final String KEY_ROWID = "_id";
17 public static final String KEY_EVENTTITLE = "title";
18 public static final String KEY_EVENTDATE = "date";
19 public static final String KEY_EVENTTIME = "time";
20 public static final String KEY_EVENTDESCRIPTION = "description";
21
22 private static final String DATABASE_NAME = "Events";
23 private static final String DATABASE_TABLE = "Event_Details";
24
25 public static final String KEY_ROWID1 = "_id";
26 public static final String KEY_CAMPUS = "campus";
27 public static final String KEY_LOCATION = "location";
28 public static final String KEY_DIRECTIONS = "direction";
29
30 private static final String DB_NAME = "Facilities";
31 private static final String DB_TABLE = "Facilities_Details";
32
33 private static final String TAG = "DbAdapter";
34
35
36 private static final int DATABASE_VERSION = 2;
37 //Create both database tables
38 private static final String DATABASE_CREATE =
39 "create table " + DATABASE_TABLE +
40 " ( " + KEY_ROWID + " integer primary key autoincrement, " +
41 KEY_EVENTTITLE + " text not null, " +
42 KEY_EVENTDATE + " text not null, " +
43 KEY_EVENTTIME + " text not null, " +
44 KEY_EVENTDESCRIPTION + " text not null);";
45
46 private static final String DATABASE_CREATE1 =
47 "create table " + DB_TABLE +
48 " ( " + KEY_ROWID1 + " integer primary key autoincrement, " +
49 KEY_CAMPUS + " text, " +
50 KEY_LOCATION + " text, " +
51 KEY_DIRECTIONS + " text);";
52
53 private final Context context;
54
55 private DatabaseHelper DBHelper;
56 private SQLiteDatabase db;
57 // constructor for your class
58 public DatabaseManager(Context ctx)
59 {
60 // Context is a way that Android transfers info about Activities and apps.
61 this.context = ctx;
62 DBHelper = new DatabaseHelper(context);
63 }
64
65 private class DatabaseHelper extends SQLiteOpenHelper
66 {
67
68 DatabaseHelper(Context context)
69 {
70 super(context, DATABASE_NAME, null, DATABASE_VERSION);
71 }
72 @Override
73 public void onCreate(SQLiteDatabase db)
74 {
75 // create databases
76 db.execSQL(DATABASE_CREATE);
77 db.execSQL(DATABASE_CREATE1);
78 //inserts into facilities database
79 insertFacilities("Kevin Street", "Basement", "Go to basement in Kevin Street Main building, prayer room is on your left");
80 insertFacilities("Aungier Street", "4th Floor", "Go to 4th floor in Aungier street, prayer room is on your right");
81 }
82 @Override
83 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
84 {
85 Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
86 db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
87 db.execSQL("DROP TABLE IF EXISTS " + DB_NAME);
88 onCreate(db);
89 }
90 } // end of the help class
91
92
93 //---opens the database---
94 public DatabaseManager open()
95 {
96
97 try{
98 db = DBHelper.getWritableDatabase();
99 }catch(SQLException e) {
100 Log.e("error", "Failed to open database: " + e);
101 }
102
103 return this;
104 }
105
106 //---closes the database---
107 public void close()
108 {
109 DBHelper.close();
110 }
111
112 //---insert an event into the database---
113 public long insertEvent(String title, String date, String time, String description)
114 {
115 ContentValues initialValues = new ContentValues();
116 initialValues.put(KEY_EVENTTITLE, title);
117 initialValues.put(KEY_EVENTDATE, date);
118 initialValues.put(KEY_EVENTTIME, time);
119 initialValues.put(KEY_EVENTDESCRIPTION, description);
120 return db.insert(DATABASE_TABLE, null, initialValues);
121 }
122 //---insert a facility into the database---
123 public long insertFacilities(String c, String d, String l) {
124 ContentValues initialValues = new ContentValues();
125 initialValues.put(KEY_CAMPUS, c);
126 initialValues.put(KEY_LOCATION, d);
127 initialValues.put(KEY_DIRECTIONS, l);
128 return db.insert(DB_TABLE, null, initialValues);
129 }
130 //---deletes a particular event---
131 public boolean deleteEvent(long rowId) {
132 // delete statement. If any rows deleted (i.e. >0), returns true
133
134 return db.delete(DATABASE_TABLE, KEY_ROWID + " = " + rowId, null) > 0;
135
136 }
137 //---retrieves all the rows for events---
138 public Cursor getAllEvents()
139 {
140 Cursor mCursor = null;
141 try{
142 mCursor = db.query(DATABASE_TABLE, new String[] {
143 KEY_ROWID,
144 KEY_EVENTTITLE,
145 KEY_EVENTDATE,
146 KEY_EVENTTIME,
147 KEY_EVENTDESCRIPTION},
148 null,
149 null,
150 null,
151 null,
152 null);
153
154 }catch (SQLException e){
155 Log.e("error", "Failed to get all events: " + e);
156 }
157 return mCursor;
158
159
160 }
161 //---retrieves all the rows for facilities---
162 public Cursor getAllFacilities()
163 {
164 Cursor mCursor = null;
165 String q = "SELECT * FROM " + DB_TABLE + ";";
166
167 try
168 {
169 mCursor = db.rawQuery(q, null);
170
171 if(mCursor != null)
172 {
173 mCursor.moveToFirst();
174 }
175 return mCursor;
176 }
177 catch(SQLException e)
178 {
179 Log.e("Error", "Failed to get item " + e);
180 }
181 return mCursor;
182 }
183 //---retrieves a particular row---
184 public Cursor getEvent(long rowId) throws SQLException
185 {
186 Cursor mCursor =
187 db.query(true, DATABASE_TABLE, new String[] {
188 KEY_ROWID,
189 KEY_EVENTTITLE,
190 KEY_EVENTDATE,
191 KEY_EVENTTIME,
192 KEY_EVENTDESCRIPTION},
193 KEY_ROWID + "=" + rowId,
194 null,
195 null,
196 null,
197 null,
198 null);
199 if (mCursor != null) {
200 mCursor.moveToFirst();
201 }
202 return mCursor;
203 }
204 //---updates an event---
205 public boolean updateEvent(long rowId, String title,
206 String date, String time, String description)
207 {
208 ContentValues args = new ContentValues();
209 args.put(KEY_EVENTTITLE, title);
210 args.put(KEY_EVENTDATE, date);
211 args.put(KEY_EVENTTIME, time);
212 args.put(KEY_EVENTDESCRIPTION, description);
213
214 return db.update(DATABASE_TABLE, args,
215 KEY_ROWID + "=" + rowId, null) > 0;
216 }
217
218
219
220}