· 8 years ago · Feb 04, 2018, 11:22 PM
1import android.content.ContentValues;
2import android.database.Cursor;
3import android.database.SQLException;
4import android.util.Log;
5import android.content.Context;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8
9import java.util.ArrayList;
10
11import static android.R.attr.factor;
12import static android.R.attr.tag;
13
14/**
15 * Created by CLAB1-PC on 1/29/2018.
16 */
17
18public class DBManager {
19 //Step 1
20 Context context;
21 private SQLiteDatabase db;
22
23 private final String DBNAME = "attendance_tbl";
24 private final String STUDENTTBL = "student_tbl";
25 private final String SECTION = "section_tbl";
26 private final String AGE = "age_tbl";
27 private final String GRADE = "grade_tbl";
28 private final String DATE = "date_tbl";
29 private final int DBVER = 1;
30
31
32 DBManager(Context context) {
33 this.context = context;
34 CustomHelper helper = new CustomHelper(context);
35 this.db = helper.getWritableDatabase();
36 }
37
38 //Step 2
39 private class CustomHelper extends SQLiteOpenHelper {
40
41 public CustomHelper(Context context) {
42 super(context, DBNAME, null, DBVER);
43 }
44
45 @Override
46 public void onCreate(SQLiteDatabase db) {
47 String sql = "create table " + STUDENTTBL +
48 " ( " + SECTION + " integer primary key autoincrement not null,"
49 + AGE + " text," + GRADE + DATE + "text);";
50 db.execSQL(sql);
51 }
52
53 @Override
54 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
55 db.execSQL("DROP TABLE IF EXISTS " + STUDENTTBL);
56 onCreate(db);
57 }
58 }
59
60 public void add(String section, String age) {
61
62 ContentValues values = new ContentValues();
63 values.put(SECTION, section);
64 values.put(AGE, age);
65 try {
66 db.insert(STUDENTTBL, null, values);
67 } catch (Exception e) {
68 Log.e("DB ERROR: ADD", e.toString());
69 e.printStackTrace();
70 }
71 }
72
73 public void delete(long id) {
74 try {
75 db.delete(STUDENTTBL, SECTION + '=' + id, null);
76
77 } catch (Exception e) {
78 Log.e("DB ERROR: DELETE", e.toString());
79 e.printStackTrace();
80 }
81
82 }
83
84 public ArrayList<ArrayList<Object>> getAllQuiz() {
85 ArrayList<ArrayList<Object>> dataArrays = new ArrayList<>();
86 Cursor cursor;
87 try {
88
89 cursor = db.query(
90 STUDENTTBL,
91 new String[]{SECTION, AGE, GRADE, DATE},
92 null, null, null, null, null
93 );
94 cursor.moveToFirst();
95 if (!cursor.isAfterLast()) {
96 do {
97 ArrayList<Object> dataList = new ArrayList<Object>();
98 dataList.add(cursor.getLong(0));
99 dataList.add(cursor.getLong(1));
100 dataList.add(cursor.getLong(2));
101 dataArrays.add(dataList);
102 }
103
104 while (cursor.moveToNext());
105
106 }
107 } catch (SQLException e) {
108 Log.e("DB Error: GET ALL NAMES", e.toString());
109 e.printStackTrace();
110 }
111
112 return dataArrays;
113 }
114
115 public ArrayList<Object> getQuiz(long id) {
116 ArrayList<Object> rowArray = new ArrayList<>();
117 Cursor cursor;
118 try {
119 cursor = db.query
120 (
121 STUDENTTBL,
122 new String[]{SECTION, AGE, GRADE, DATE},
123 SECTION + "=" + id,
124 null, null, null, null, null
125 );
126 cursor.moveToFirst();
127 if (!cursor.isAfterLast()) {
128 do {
129 rowArray.add(cursor.getLong(0)); //SECTION
130 rowArray.add(cursor.getLong(1)); //AGE
131 rowArray.add(cursor.getLong(2)); //GRADE
132 rowArray.add(cursor.getLong(3)); //DATE
133 }
134
135 while (cursor.moveToNext());
136 }
137 cursor.close();
138
139 } catch (SQLException e) {
140 Log.e("DB Error: GET ALL NAMES", e.toString());
141 e.printStackTrace();
142 }
143 return rowArray;
144 }
145
146 public void update(long id, String age, String section, String grade,) {
147 ContentValues values = new ContentValues();
148 values.put(AGE, age);
149 values.put(SECTION, section);
150 values.put(GRADE, grade);
151 try {
152 db.update(STUDENTTBL, values, SECTION + " = " + id, null);
153 } catch (Exception e) {
154 Log.e("DB Error: EDIT", e.toString());
155 e.printStackTrace();
156 }
157 }
158}