· 9 years ago · Nov 02, 2016, 04:06 PM
1/**
2 * Database helper. All database (sqlite) calls can go in here
3 *
4 * @author Muhammad Riyaz
5 *
6 */
7 public class DBHelper {
8
9 private static final String DB_NAME = "db_name";
10 private static final int DB_VER = 1;
11 private static final String TAG = DBhelper.class.getSimpleName();
12
13 // table names
14 private static final String TABLE_POSTS = "posts";
15
16 // field names.
17 public static final String C_POST_ID = "post_id";
18 public static final String C_POST_URL = "url";
19 public static final String C_POST_TITLE = "title";
20 public static final String C_POST_CONTENT = "content";
21
22
23 // Initializations
24 private static DBOpenHelper ourHelper;
25 private static SQLiteDatabase ourDataBase;
26 private int openCounter = 0;
27 private static DBhelper ourInstance;
28
29 public static class DBOpenHelper extends SQLiteOpenHelper {
30 public DBOpenHelper(Context context) {
31 super(context, DB_NAME, null, DB_VER);
32 }
33
34 @Override
35 public void onCreate(SQLiteDatabase db) {
36 // create table queries
37 String sql_posts = String
38 .format("create TABLE %s (%s INTEGER PRIMARY KEY, %s TEXT,%s TEXT," +
39 "%s TEXT)",
40 TABLE_POSTS, C_POST_ID, C_POST_TITLE,
41 C_POST_URL, C_POST_CONTENT);
42 Log.d(TAG, "onCreate sql: " + sql_posts);
43 db.execSQL(sql_posts);
44
45
46
47 }
48
49 @Override
50 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
51 // write here the queries to be executed when the DB_VER upgrades
52 db.execSQL("drop table if exists " + TABLE_POSTS);
53 Log.d(TAG, "On upgrade destroyed table " + TABLE_POSTS);
54
55 this.onCreate(db);
56 }
57
58 }
59
60 public static synchronized void initDB(Context context) {
61 if (ourInstance == null) {
62 ourInstance = new DBhelper();
63 ourHelper = new DBOpenHelper(context);
64 }
65 }
66
67 public static synchronized DBhelper getInstance() {
68 if (ourInstance == null) {
69 throw new IllegalStateException(
70 DBhelper.class.getSimpleName()
71 + " is not initialized, call initializeInstance(..) method first.");
72 }
73
74 return ourInstance;
75 }
76
77 public synchronized DBhelper writeOpen() {
78 openCounter++;
79 if (openCounter == 1) {
80 Log.i(TAG, "new write open");
81 ourDataBase = ourHelper.getWritableDatabase();
82 } else {
83 Log.i(TAG, "old write open");
84 }
85 return this;
86 }
87
88 public synchronized DBhelper readOpen() {
89 openCounter++;
90 if (openCounter == 1) {
91 Log.i(TAG, "new read open");
92 ourDataBase = ourHelper.getReadableDatabase();
93 } else {
94 Log.i(TAG, "old read open");
95 }
96 return this;
97 }
98
99 public synchronized void closeDatabase() {
100 openCounter--;
101 if (openCounter == 0) {
102 Log.i(TAG, "closing db");
103 // Closing database
104 ourDataBase.close();
105 // ourDataBase.releaseReference();
106
107 } else {
108 Log.i(TAG, "not closing db. Open connections: " + openCounter);
109 }
110 }
111
112 public ArrayList<Post> getPosts(){
113 ArrayList<Post> postsArrayList = new ArrayList<>();
114 Cursor posts = ourDataBase.query(TABLE_1, null, null, null, null,
115 null, null);
116 if (posts != null && posts.getCount() > 0) {
117 for (int i = 0; i < posts.getCount(); i++) {
118 posts.moveToPosition(i);
119 postsArrayList.add(new Post(
120 posts.getLong(posts.getColumnIndex(DBHelper.C_POST_ID)),
121 posts.getString(posts.getColumnIndex(DBHelper.C_POST_TITLE)),
122 posts.getString(posts.getColumnIndex(DBHelper.C_POST_CONTENT)),
123 posts.getString(posts.getColumnIndex(DBHelper.C_POST_URL))));
124 }
125 return postsArrayList;
126 } else {
127 return null;
128 }
129 }
130
131 public long insertPost(long postID, String postTitle,
132 String postURL, String postContent) {
133 ContentValues cv = new ContentValues();
134 cv.put(C_POST_ID, postID);
135 cv.put(C_POST_TITLE, postTitle);
136 cv.put(C_POST_URL, postURL);
137 cv.put(C_POST_CONTENT, postContent);
138 Log.i(TAG, "inserted post " + postID + " title: " + postTitle);
139 return ourDataBase.insert(TABLE_POSTS, null, cv);
140 }
141 }