· 9 years ago · Dec 18, 2016, 08:56 PM
1package uk.ac.kent.jt365.imageviewer;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.SQLException;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteOpenHelper;
9import android.util.Log;
10
11import java.util.ArrayList;
12
13
14public class DataBaseHelper extends SQLiteOpenHelper {
15 private static final String TAG = "DbHelper";
16 private static final String DATABASE_NAME = "favorites.db";
17 private static final String TABLE_NAME = "favorites_table";
18 private static final String _ID = "_id";
19 private static final String IMAGE = "image";
20 private static final String TITLE = "title";
21 private static final String AUTHOR = "author";
22
23 private static DataBaseHelper mDataBaseHelper;
24
25 public static synchronized DataBaseHelper getInstance(Context context) {
26 // Use application context, which will ensure that we don't leak Activity context
27 if (mDataBaseHelper == null) {
28 mDataBaseHelper = new DataBaseHelper(context.getApplicationContext());
29 }
30 return mDataBaseHelper;
31 }
32
33 private DataBaseHelper(Context context) {
34 super(context, DATABASE_NAME, null, 1);
35 }
36
37 @Override
38 public void onCreate(SQLiteDatabase db) {
39 // SQL syntax for creating the table
40 String CREATE_PHOTO_TABLE = ("CREATE TABLE " + TABLE_NAME
41 + "("
42 + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
43 + IMAGE + " TEXT,"
44 + TITLE + " TEXT,"
45 + AUTHOR + " TEXT"
46 + ")"
47 );
48
49 // Create the table
50 db.execSQL(CREATE_PHOTO_TABLE);
51 }
52
53 @Override
54 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
55 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
56 onCreate(db);
57 }
58
59
60 // Insert image data into database
61 public void insertPhotoData(FavoritesData favoritesData) {
62 SQLiteDatabase db = getWritableDatabase();
63 db.beginTransaction();
64
65 try {
66 ContentValues values = new ContentValues();
67 values.put(IMAGE, favoritesData.image); // Put photo bitmap string
68 values.put(TITLE, favoritesData.title); // Put photo title
69 values.put(AUTHOR, favoritesData.author); // Put photo title
70
71 db.insertOrThrow(TABLE_NAME, null, values);
72 db.setTransactionSuccessful();
73 } catch (SQLException e) {
74 e.printStackTrace();
75 Log.d(TAG, "Could not add photo to database");
76 } finally {
77 db.endTransaction();
78 }
79 }
80
81 // Get all data from Table
82 public ArrayList<FavoritesData> getAllData() {
83 ArrayList<FavoritesData> favoritesDetails = new ArrayList<>();
84
85 String DETAIL_SELECT_QUERY = "SELECT * FROM " + TABLE_NAME;
86
87 SQLiteDatabase db = getReadableDatabase();
88 Cursor cursor = db.rawQuery(DETAIL_SELECT_QUERY, null);
89
90 try {
91 if (cursor.moveToFirst()) {
92 do {
93 FavoritesData favoritesData = new FavoritesData();
94 favoritesData.image = cursor.getString(cursor.getColumnIndex(IMAGE));
95 favoritesData.title = cursor.getString(cursor.getColumnIndex(TITLE));
96 favoritesData.author = cursor.getString(cursor.getColumnIndex(AUTHOR));
97
98 favoritesDetails.add(favoritesData);
99 } while (cursor.moveToNext());
100 }
101 } catch (Exception e) {
102 Log.d(TAG, "Error while trying to fetch from database");
103 } finally {
104 if (cursor != null && !cursor.isClosed()) {
105 cursor.close();
106 }
107 }
108
109 return favoritesDetails;
110 }
111}