- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseHelper.java
More file actions
Latest commit
75 lines (57 loc) · 2.21 KB
/
Copy pathDatabaseHelper.java
File metadata and controls
75 lines (57 loc) · 2.21 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
packagecom.example.weighttracker;
importandroid.annotation.TargetApi;
importandroid.content.ContentValues;
importandroid.content.Context;
importandroid.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.database.sqlite.SQLiteOpenHelper;
importandroidx.annotation.Nullable;
publicclassDatabaseHelperextendsSQLiteOpenHelper {
publicstaticfinalStringDATABASE_NAME = "Weights.db";
publicstaticfinalStringTABLE_NAME = "Weights_Table";
publicstaticfinalStringCOL_1 = "ID";
publicstaticfinalStringCOL_2 = "DATE";
publicstaticfinalStringCOL_3 = "WEIGHT";
publicDatabaseHelper(@NullableContextcontext) {
super(context, DATABASE_NAME, null, 1);
}
@Override
publicvoidonCreate(SQLiteDatabasedb) {
db.execSQL("create table "+TABLE_NAME+" (ID INTEGER PRIMARY KEY AUTOINCREMENT,DATE TEXT,WEIGHT REAL)");
}
@Override
publicvoidonUpgrade(SQLiteDatabasedb, intoldVersion, intnewVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
publicbooleaninsertdata(Stringdate, Stringweight){
SQLiteDatabasedb = this.getReadableDatabase();
ContentValuescv = newContentValues();
cv.put(COL_2,date);
cv.put(COL_3,weight);
longresult = db.insert(TABLE_NAME,null,cv);
if(result==-1){
returnfalse;
}else{
returntrue;
}
}
publicCursorgetallData(){
SQLiteDatabasedb = this.getReadableDatabase();
Cursorcr = db.rawQuery("select * from "+ TABLE_NAME,null);
returncr;
}
publicbooleanUpdateData(Stringid,Stringdate, Stringweight){
SQLiteDatabasedb = this.getReadableDatabase();
ContentValuescv = newContentValues();
cv.put(COL_1,id);
cv.put(COL_2,date);
cv.put(COL_3,weight);
db.update(TABLE_NAME,cv,"ID = ?",newString[] { id });
returntrue;
}
publicintdeleteData(Stringid){
SQLiteDatabasedb = this.getReadableDatabase();
returndb.delete(TABLE_NAME,"ID = ?",newString[] { id });
}
}