· 6 years ago · Apr 10, 2019, 07:56 AM
1
2/**
3 * Created By Pradip Vishwakarma on 07/02/2018
4 */
5public class DatabaseOpenHelper extends SQLiteOpenHelper {
6
7 private static DatabaseOpenHelper _instance;
8 private static SQLiteDatabase _database;
9
10 public DatabaseOpenHelper(@Nullable Context context) {
11 super(context, Constants.DatabaseHelper.DATABASE_NAME, null, DATABASE_VERSION_4);
12 }
13
14 public static SQLiteDatabase getInstance(Context context) {
15 if (_instance == null) {
16 _instance = new DatabaseOpenHelper(context);
17 }
18 if (_database == null) {
19 _database = _instance.getWritableDatabase();
20 }
21 return _database;
22 }
23
24 @Override
25 public void onCreate(SQLiteDatabase db) {
26 if (!db.isReadOnly()) {
27 db.execSQL("PRAGMA foreign_keys=ON;");
28 }
29
30 //Create Contract Master
31 db.execSQL("CREATE TABLE IF NOT EXISTS " + Table1.TABLE_NAME + " (" +
32 Table1.COL1 + " TEXT, " +
33 Table1.COL2 + " TEXT, " +
34 Table1.COL3 + " TEXT, " +
35 Table1.COL4 + " TEXT " +
36
37 ")"
38 );
39
40 //Create Table Time Session Map
41 db.execSQL("CREATE TABLE IF NOT EXISTS " + Table2.TABLE_NAME + " (" +
42 Table2.COL1 + " TEXT, " +
43 Table2.COL2 + " TEXT, " +
44 Table2.COL3 + " TEXT, " +
45 Table2.COL4 + " TEXT " +
46
47 ")"
48 );
49 }
50
51 @Override
52 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
53 switch (oldVersion) {
54 case DATABASE_VERSION_1:
55 runVersion2UpdateScript(db);
56 case DATABASE_VERSION_2:
57 runVersion3UpdateScript(db);
58 case DATABASE_VERSION_3:
59 runVersion4UpdateScript(db);
60 }
61
62 }
63
64 private void runVersion4UpdateScript(SQLiteDatabase database) {
65 try {
66 database.execSQL("ALTER TABLE " + SQLiteTableHelper.Table1.TABLE_NAME
67 + " ADD COLUMN " + Table1.COL5+ " TEXT ");
68 } catch (SQLException e) {
69 Logger.log(e);
70 }
71 }
72}
73
74
75
76public class SQLiteTableHelper {
77
78 public interface Table1 {
79 String TABLE_NAME = "table_one";
80 String COL1 = "col_one";
81 String COL2 = "col_two";
82 String COL3 = "col_three";
83 String COL4 = "col_four";
84 String COL5 = "col_five";
85 }
86
87 public interface Table2 {
88 String TABLE_NAME = "table_two";
89 String COL1 = "col_one";
90 String COL2 = "col_two";
91 String COL3 = "col_three";
92 String COL4 = "col_four";
93 }
94}
95
96
97public class DatabaseUtils {
98
99 public static POJO getTableOnebyId(Context context, String someId) {
100 POJO pojo = new POJO();
101
102 if (!TextUtils.isEmpty(someId)) {
103 SQLiteDatabase sqLiteDatabase = DatabaseOpenHelper.getInstance(context);
104 String query = "SELECT * FROM " + SQLiteTableHelper.Table1.TABLE_NAME +
105 " WHERE " + SQLiteTableHelper.Table1.COL1 + " = '" + someId + "'";
106 Cursor cursor = sqLiteDatabase.rawQuery(query, null);
107 if (cursor != null) {
108 cursor.moveToFirst();
109 while (!cursor.isAfterLast()) {
110 pojo = getPojoTableOne(cursor);
111 cursor.moveToNext();
112 }
113 cursor.close();
114 }
115 }
116 return pojo;
117 }
118
119 private static POJO getPojoTableOne(Cursor cursor) {
120 POJO pojo = new POJO();
121
122 String col1 = cursor.getString(cursor.getColumnIndex(SQLiteTableHelper.Table1.COL1));
123 pojo.setCol1(col1);
124
125 String col2 = cursor.getString(cursor.getColumnIndex(SQLiteTableHelper.Table1.COL2));
126 pojo.setCol2(col2);
127
128 String col3 = cursor.getString(cursor.getColumnIndex(SQLiteTableHelper.Table1.COL3));
129 pojo.setCol3(col3);
130
131 String col4 = cursor.getString(cursor.getColumnIndex(SQLiteTableHelper.Table1.COL4));
132 pojo.setCol4(col4);
133
134 String col5 = cursor.getString(cursor.getColumnIndex(SQLiteTableHelper.Table1.COL5));
135 pojo.setCol5(col5);
136
137 return pojo;
138 }
139
140
141 public static void insertorupdateTable1(Context context, POJO pojo) {
142 try {
143
144 SQLiteDatabase database = DatabaseOpenHelper.getInstance(context);
145 ContentValues values = new ContentValues();
146 values.put(SQLiteTableHelper.Table1.COL1, pojo.getCol1());
147 values.put(SQLiteTableHelper.Table1.COL2, pojo.getCol2());
148 values.put(SQLiteTableHelper.Table1.COL3, pojo.getCol3());
149 values.put(SQLiteTableHelper.Table1.COL4, pojo.getCol4());
150 values.put(SQLiteTableHelper.Table1.COL5, pojo.getCol5());
151
152 boolean isExists = isExists(database, SQLiteTableHelper.Table1.TABLE_NAME,
153 SQLiteTableHelper.Table1.COL1, pojo.getCol1());
154
155 if (isExists) {
156
157 database.update(SQLiteTableHelper.Table1.TABLE_NAME, values,
158 SQLiteTableHelper.Table1.COL1 + "=? ",
159 new String[]{pojo.getCol1()});
160 } else {
161 database.insert(SQLiteTableHelper.Table1.TABLE_NAME, null, values);
162 }
163 } catch (Exception e) {
164 Logger.log(e);
165 }
166 }
167
168 public static boolean isExists(SQLiteDatabase database, String table, String columnName, String value) {
169 boolean isExists = false;
170 Cursor cursor = database.query(table, null, columnName + "=?", new String[]{value}, null, null, null);
171 if (cursor != null) {
172 if (cursor.getCount() > 0) {
173 isExists = true;
174 }
175 }
176 cursor.close();
177 return isExists;
178 }
179
180}
181
182public class POJO implements Serializable,Clonable{
183 private String col1,col2,col3,col4;
184
185 public String getCol1() {
186 return col1;
187 }
188
189 public void setCol1(String col1) {
190 this.col1 = col1;
191 }
192
193 public String getCol2() {
194 return col2;
195 }
196
197 public void setCol2(String col2) {
198 this.col2 = col2;
199 }
200
201 public String getCol3() {
202 return col3;
203 }
204
205 public void setCol3(String col3) {
206 this.col3 = col3;
207 }
208
209 public String getCol4() {
210 return col4;
211 }
212
213 public void setCol4(String col4) {
214 this.col4 = col4;
215 }
216
217}