-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBHelper.java
More file actions
285 lines (253 loc) · 9.17 KB
/
Copy pathDBHelper.java
File metadata and controls
285 lines (253 loc) · 9.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package com.app.yourrestaurantapp.utilities;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
public class DBHelper extends SQLiteOpenHelper {
private final static String DB_NAME = "restaurant_db";
public final static int DB_VERSION = 1;
public static SQLiteDatabase db;
private final Context context;
private String DB_PATH;
private final String TABLE_CART = "tbl_cart";
private final String CART_ID = "id";
private final String PRODUCT_NAME = "product_name";
private final String QUANTITY = "quantity";
private final String TOTAL_PRICE = "total_price";
private final String PRODUCT_IMAGE = "product_image";
private final String PRODUCT_DATE = "date_time";
private final String TABLE_HISTORY = "tbl_history";
private final String HISTORY_ID = "id";
private final String CODE = "code";
private final String ORDER_LIST = "order_list";
private final String ORDER_TOTAL = "order_total";
private final String DATE_TIME = "date_time";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
SQLiteDatabase db_Read = null;
if (dbExist) {
} else {
db_Read = this.getReadableDatabase();
db_Read.close();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database: " + e.getMessage());
}
}
}
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException {
InputStream myInput = context.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public void close() {
db.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public ArrayList<ArrayList<Object>> getAllData() {
ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
Cursor cursor = null;
try {
cursor = db.query(TABLE_CART, new String[]{CART_ID, PRODUCT_NAME, QUANTITY, TOTAL_PRICE, PRODUCT_IMAGE, PRODUCT_DATE},
null, null, null, null, "date_time DESC");
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
do {
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cursor.getLong(0));
dataList.add(cursor.getString(1));
dataList.add(cursor.getString(2));
dataList.add(cursor.getString(3));
dataList.add(cursor.getString(4));
dataList.add(cursor.getString(5));
dataArrays.add(dataList);
}
while (cursor.moveToNext());
}
cursor.close();
} catch (SQLException e) {
Log.e("DB Error", e.toString());
e.printStackTrace();
}
return dataArrays;
}
public ArrayList<ArrayList<Object>> getAllDataHistory() {
ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
Cursor cursor = null;
try {
cursor = db.query(TABLE_HISTORY, new String[]{HISTORY_ID, CODE, ORDER_LIST, ORDER_TOTAL, DATE_TIME},
null, null, null, null, "id DESC");
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
do {
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cursor.getLong(0));
dataList.add(cursor.getString(1));
dataList.add(cursor.getString(2));
dataList.add(cursor.getString(3));
dataList.add(cursor.getString(4));
dataArrays.add(dataList);
}
while (cursor.moveToNext());
}
cursor.close();
} catch (SQLException e) {
Log.e("DB Error", e.toString());
e.printStackTrace();
}
return dataArrays;
}
public boolean isDataExist(long id) {
boolean exist = false;
Cursor cursor = null;
try {
cursor = db.query(TABLE_CART, new String[]{CART_ID}, CART_ID + "=" + id, null, null, null, null);
if (cursor.getCount() > 0) {
exist = true;
}
cursor.close();
} catch (SQLException e) {
Log.e("DB Error", e.toString());
e.printStackTrace();
}
return exist;
}
public boolean isDataHistoryExist(long id) {
boolean exist = false;
Cursor cursor = null;
try {
cursor = db.query(TABLE_HISTORY, new String[]{HISTORY_ID}, HISTORY_ID + "=" + id, null, null, null, null);
if (cursor.getCount() > 0) {
exist = true;
}
cursor.close();
} catch (SQLException e) {
Log.e("DB Error", e.toString());
e.printStackTrace();
}
return exist;
}
public boolean isPreviousDataExist() {
boolean exist = false;
Cursor cursor = null;
try {
cursor = db.query(TABLE_CART, new String[]{CART_ID}, null, null, null, null, null);
if (cursor.getCount() > 0) {
exist = true;
}
cursor.close();
} catch (SQLException e) {
Log.e("DB Error", e.toString());
e.printStackTrace();
}
return exist;
}
public void addData(long id, String product_name, int quantity, double total_price, String product_image, String date_time) {
ContentValues values = new ContentValues();
values.put(CART_ID, id);
values.put(PRODUCT_NAME, product_name);
values.put(QUANTITY, quantity);
values.put(TOTAL_PRICE, total_price);
values.put(PRODUCT_IMAGE, product_image);
values.put(PRODUCT_DATE, date_time);
try {
db.insert(TABLE_CART, null, values);
} catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void addDataHistory(String code, String order_list, String order_total, String date_time) {
ContentValues values = new ContentValues();
values.put(CODE, code);
values.put(ORDER_LIST, order_list);
values.put(ORDER_TOTAL, order_total);
values.put(DATE_TIME, date_time);
try {
db.insert(TABLE_HISTORY, null, values);
} catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void deleteData(long id) {
try {
db.delete(TABLE_CART, CART_ID + "=" + id, null);
} catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void deleteDataHistory(long id) {
try {
db.delete(TABLE_HISTORY, HISTORY_ID + "=" + id, null);
} catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void deleteAllData() {
try {
db.delete(TABLE_CART, null, null);
} catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void deleteAllDataHistory() {
try {
db.delete(TABLE_HISTORY, null, null);
} catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void updateData(long id, int quantity, double total_price) {
ContentValues values = new ContentValues();
values.put(QUANTITY, quantity);
values.put(TOTAL_PRICE, total_price);
try {
db.update(TABLE_CART, values, CART_ID + "=" + id, null);
} catch (Exception e) {
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}
}