· 6 years ago · Oct 23, 2019, 02:24 PM
1package com.quizgame.universalquiz.helper;
2
3import android.content.Context;
4import android.database.Cursor;
5import android.database.sqlite.SQLiteDatabase;
6import android.database.sqlite.SQLiteException;
7import android.database.sqlite.SQLiteOpenHelper;
8import android.util.Log;
9
10import java.io.File;
11import java.io.FileOutputStream;
12import java.io.IOException;
13import java.io.InputStream;
14import java.io.OutputStream;
15
16public class DBHelper extends SQLiteOpenHelper {
17
18 //database version
19 private static int db_version = 1;
20
21 //database name
22 private static String db_name = "quiz_level.db";
23
24 //database path string
25 private String db_path;
26
27
28 private SQLiteDatabase db;
29 private final Context context;
30
31 //table name
32 public static String TABLE_NAME = "level";
33
34 //column names
35
36 public static String CATE_ID = "cat_id";
37 public static String SUB_CATE_ID = "sub_cat_id";
38 public static String LEVEL_NO = "level_no";
39
40
41 public DBHelper(Context context) {
42 super(context, db_name, null, db_version);
43 this.context = context;
44
45 File database = context.getDatabasePath(db_name);
46 if (!database.exists()) {
47 // Database does not exist so copy it from assets here
48 db_path = context.getDatabasePath(db_name).toString().replace(db_name, "");
49
50 } else {
51 //if database exist get database from path
52 db_path = context.getDatabasePath(db_name).toString();
53
54 }
55
56 }
57
58 @Override
59 public SQLiteDatabase getReadableDatabase() {
60 return super.getReadableDatabase();
61 }
62
63 @Override
64 public SQLiteDatabase getWritableDatabase() {
65 return super.getWritableDatabase();
66 }
67
68 @Override
69 public void onCreate(SQLiteDatabase db) {
70
71 }
72
73 @Override
74 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
75
76 }
77
78 //delete database
79 public void db_delete() {
80 File file = new File(db_path + db_name);
81 if (file.exists()) {
82 file.delete();
83
84 }
85 }
86
87 //Create a empty database on the system
88 public void createDatabase() throws IOException {
89 boolean dbExist = checkDataBase();
90
91 if (dbExist) {
92 Log.v("DB Exists", "db exists");
93
94 }
95
96 boolean dbExist1 = checkDataBase();
97 if (!dbExist1) {
98 this.getWritableDatabase();
99 try {
100 this.close();
101 copyDataBase();
102 } catch (IOException e) {
103 throw new Error("Error copying database");
104 }
105 }
106 }
107
108 //Check database already exist or not
109 private boolean checkDataBase() {
110 boolean checkDB = false;
111 try {
112 String myPath = db_path + db_name;
113 File dbfile = new File(myPath);
114 checkDB = dbfile.exists();
115 } catch (SQLiteException e) {
116 }
117 return checkDB;
118 }
119
120 //Copies your database from your local assets-folder to the just created empty database in the system folder
121 private void copyDataBase() throws IOException {
122 String outFileName = db_path + db_name;
123 OutputStream myOutput = new FileOutputStream(outFileName);
124 InputStream myInput = context.getAssets().open(db_name);
125
126 byte[] buffer = new byte[1024];
127 int length;
128 while ((length = myInput.read(buffer)) > 0) {
129 myOutput.write(buffer, 0, length);
130 }
131 myInput.close();
132 myOutput.flush();
133 myOutput.close();
134 }
135
136 /*
137 * insert level no
138 */
139 public void insertIntoDB(int cat_id, int sub_cat_id, int level_no) {
140 SQLiteDatabase db = this.getWritableDatabase();
141 String query = "INSERT INTO " + TABLE_NAME + " (" + CATE_ID + "," + SUB_CATE_ID + "," + LEVEL_NO + ") VALUES('" + cat_id + "', '" + sub_cat_id + "', '" + level_no + "');";
142 db.execSQL(query);
143
144 }
145
146 /*
147 *with this method we check if categoryId & subCategoryId is already exist or not in our database
148 */
149 public boolean isExist(int cat_id, int sub_cat_id) {
150 db = this.getReadableDatabase();
151 Cursor cur = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE ( " + CATE_ID + " = " + cat_id + " AND " + SUB_CATE_ID + " = " + sub_cat_id + ")", null);
152 boolean exist = (cur.getCount() > 0);
153 cur.close();
154 System.out.println("---isExit " + (cur.getCount() > 0));
155 return exist;
156
157 }
158
159 /*
160 * get level
161 */
162 public int GetLevelById(int cat_id, int sub_cat_id) {
163 int level = 1;
164 String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE (" + CATE_ID + "=" + cat_id + " AND " + SUB_CATE_ID + "=" + sub_cat_id + ")";
165 SQLiteDatabase db = this.getReadableDatabase();
166 Cursor c = db.rawQuery(selectQuery, null);
167 // looping through all rows and adding to list
168 if (c.moveToFirst()) {
169 do {
170 level = c.getInt(c.getColumnIndex(LEVEL_NO));
171 } while (c.moveToNext());
172 }
173 return level;
174 }
175
176 /*
177 * Update lavel
178 */
179 public void UpdateLevel(int cat_id, int sub_cat_id, int level_no) {
180 db = this.getReadableDatabase();
181
182 db.execSQL("update " + TABLE_NAME + " set level_no=" + level_no + " where (cat_id =" + cat_id + " and " + sub_cat_id + " = " + sub_cat_id + ")");
183 }
184}