· 7 years ago · Feb 17, 2019, 10:36 AM
1SQLiteDatabase db = this.getReadableDatabase();
2
3public StudentsinLib getId(int id){
4
5 String selectQuery = "SELECT * FROM " + STUDENTDATABASETABLE_NAME + " WHERE "+ COL + " ="+id;
6
7 SQLiteDatabase db = this.getReadableDatabase(); // ======> THE EMULATOR POINTS HERE
8 Cursor cursor = db.rawQuery(selectQuery, null);
9
10 if (cursor != null)
11 cursor.moveToFirst();
12
13 StudentsinLib studentsinLib = new StudentsinLib();
14 studentsinLib.setId(cursor.getInt(0));
15 studentsinLib.setName(cursor.getString(1));
16 studentsinLib.setNumber(cursor.getString(2));
17
18 //log
19 Log.d("getStudent(" + id + ")", StudentsinLib.toString());
20 // 5. return student
21 return studentsinLib;
22}
23
24public class StudentsinLibDatabase extends SQLiteOpenHelper {
25// Database Version
26private static final int DATABASE_VERSION = 1;
27// Database Name
28private static final String STUDENTDATABASE_NAME = "StudentsinLibDB";
29// Table Content
30private static final String STUDENTDATABASETABLE_NAME = "StudentsinLibTB";
31private static String COL = "studentsinLibId";
32private static String COL1 = "StudentsinLibName";
33private static String COL2 = "studentsinLibNumber";
34
35private static final String[] COLUMNS = {COL,COL1,COL2};
36public Context context;
37
38public StudentssinLibDatabase(Context context) {
39 super(context, STUDENTDATABASETABLE_NAME, null, DATABASE_VERSION);
40 this.context=context;
41}
42@Override
43public void onCreate(SQLiteDatabase db) {
44
45 String CREATE_TABLE = "CREATE TABLE "+STUDENTDATABASETABLE_NAME+"("
46 +COL+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
47 +COL1+" TEXT,"
48 +COL2+" TEXT);";
49
50 db.execSQL(CREATE_TABLE);
51}
52@Override
53public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
54 // Drop older students table if existed
55 db.execSQL("DROP TABLE IF EXISTS students");
56
57 // create fresh students table
58 this.onCreate(db);
59}
60public void addStudentstoLib(StudentsinLib studentstoLib){
61
62 Log.d("addStudent", studentstolib.toString());
63
64 // 1. get reference to writable DB
65 SQLiteDatabase db = this.getWritableDatabase();
66
67 // 2. create ContentValues to add key "column"/value
68
69 ContentValues values = new ContentValues();
70 values.put(COL, studentstolib.getId());
71 values.put(COL1, studentstolib.getName());
72 values.put(COL2, studentstolib.getNumber());
73
74 // 3. insert
75 db.insert(STUDENTDATABASETABLE_NAME, // table
76 null, //nullColumnHack
77 values); // key/value -> keys = column names/ values = column values
78
79 // 4. close
80 db.close();
81}
82public StudentsinLib getId(int id){
83
84 String selectQuery = "SELECT * FROM " + STUDENTDATABASETABLE_NAME + " WHERE "+ COL + " ="+id;
85
86 SQLiteDatabase db = this.getReadableDatabase(); // ======> the emulator points here
87 Cursor cursor = db.rawQuery(selectQuery, null);
88
89 if (cursor != null)
90 cursor.moveToFirst();
91
92 StudentsinLib studentsinLib = new StudentsinLib();
93 studentsinLib.setId(cursor.getInt(0));
94 studentsinLib.setName(cursor.getString(1));
95 studentsinLib.setNumber(cursor.getString(2));
96
97 //log
98 Log.d("getStudent(" + id + ")", StudentsinLib.toString());
99 // 5. return student
100 return studentsinLib;
101}
102
103public void deleteStudentinLib(int id){
104 SQLiteDatabase db = this.getWritableDatabase();
105 db.delete(StudentsinLibDatabase, COL + " = ?",
106 new String[]{String.valueOf(id)});
107 db.close();
108}
109
110public int updateStudent(StudentsinLib studentsinLib) {
111 // 1. get reference to writable DB
112 SQLiteDatabase db = this.getWritableDatabase();
113 // 2. create ContentValues to add key "column"/value
114 ContentValues values = new ContentValues();
115 values.put(COL, studentsinLib.getId());
116 values.put(COL1,studentsinLib.getName());
117 values.put(COL2,studentsinLib.getNumber());
118
119 // 3. updating row
120 int i = db.update(STUDENTDATABASETABLE_NAME, //table
121 values, // column/value
122 COL + " = ?", // selections
123 new String[]{String.valueOf(studentsinLib.getId())}); //selection args
124 // 4. close
125 db.close();
126 return i;
127}
128
129public ArrayList<StudentsinLib> getAllStudentsinLib() {
130 ArrayList<StudentsinLib> studentsinLibs = new ArrayList<StudentsinLib>();
131 // 1. build the query
132 String query = "SELECT * FROM " + STUDENTDATABASETABLE_NAME;
133 // 2. get reference to writable DB
134 SQLiteDatabase db = this.getWritableDatabase();
135 Cursor cursor = db.rawQuery(query, null);
136 // 3. go over each row, build student and add it to list
137 while (cursor.moveToNext()) {
138 StudentsinLib studentsinLib = new StudentsinLib();
139 studentsinLib.setId(cursor.getInt(0));
140 studentsinLib.setName(cursor.getString(1));
141 studentsinLib.setNumber(cursor.getString(2));
142 studentsinLibs.add(studentsinLib);
143 }
144 Log.d("getAllStudents()", studentsinLibs.toString());
145 // return student
146 return studentsinLibs;
147 }
148}