· 10 years ago · Sep 13, 2016, 01:06 PM
1package com.chronojam.mealsmore;
2
3import android.content.Context;
4import android.database.Cursor;
5import android.database.sqlite.SQLiteDatabase;
6import android.database.sqlite.SQLiteOpenHelper;
7
8
9/**
10 * This class is a helper class for the Favourites SQLite database
11 */
12public class DatabaseHelper extends SQLiteOpenHelper {
13
14 // The database name, table name, and column names
15 private static final String dbName = "FavouritesDB";
16 private static final String placeTable = "Places";
17 private static final String colName = "PlaceName";
18 private static final String colRating = "PlaceRating";
19 private static final String colAddress = "PlaceAddress";
20 private static final String colPhone = "PlacePhone";
21 private static final String colMondayHours = "MondayHours";
22 private static final String colTuesdayHours = "TuesdayHours";
23 private static final String colWednesdayHours = "WednesdayHours";
24 private static final String colThursdayHours = "ThursdayHours";
25 private static final String colFridayHours = "FridayHours";
26 private static final String colSaturdayHours = "SaturdayHours";
27 private static final String colSundayHours = "SundayHours";
28
29 /**
30 * The constructor must call the constructor of the super class
31 * @param context The context attached to the database
32 */
33 public DatabaseHelper(Context context) {
34 // The super constructor takes context: the context attached to the database, databaseName: the name of the database
35 // CursorFactory: allows extra operations and validations to be performed on the queries of the database which was not needed
36 // for this app, and the version of the database
37 super(context, dbName, null, 1);
38 }
39
40 /**
41 * Creates a new table if it doesn't already exist
42 * @param db The database to create the table
43 */
44 @Override
45 public void onCreate(SQLiteDatabase db) {
46 db.execSQL("CREATE TABLE IF NOT EXISTS " + placeTable + " (" + colAddress + " TEXT PRIMARY KEY, " + colName + " TEXT, " + colRating + " REAL, " +
47 colPhone + " TEXT, " + colMondayHours + " TEXT, " + colTuesdayHours + " TEXT, " + colWednesdayHours + " TEXT, " + colThursdayHours + " TEXT, " + colFridayHours + " TEXT, " +
48 colSaturdayHours + " TEXT, " + colSundayHours + " TEXT)");
49 }
50
51 /**
52 * When upgrading just replace the old table with the new table
53 * @param db The database
54 * @param oldVersion The previous version of the database
55 * @param newVersion The new version of the database
56 */
57 @Override
58 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
59 db.execSQL("DROP TABLE IF EXISTS "+placeTable);
60 onCreate(db);
61 }
62
63 /**
64 * Checks if the desired record is present in the database
65 * @param field The column to check
66 * @param fieldValue The value of the column (the record that is being checked for)
67 * @return Whether the record is present or not
68 */
69 public boolean isRecordInDB(String field, String fieldValue){
70 SQLiteDatabase db = getReadableDatabase();
71 String query = "SELECT * FROM "+placeTable+" WHERE "+field+" = '"+ fieldValue+"'";
72 Cursor cursor = db.rawQuery(query, null);
73 if(cursor.getCount() <= 0){
74 cursor.close();
75 db.close();
76 return false;
77 }
78 cursor.close();
79 db.close();
80 return true;
81 }
82}