· 6 years ago · May 06, 2019, 02:42 PM
1public class DatabaseManager extends SQLiteOpenHelper {
2
3
4 private static final String DATABASE_NAME = "SittingDatabase";
5 private static final int DATABASE_VERSION = 1;
6 private static final String TABLE_NAME = "SittingTime";
7 private static final String COLUMN_ID = "id";
8 private static final String COLUMN_DATE = "date";
9 private static final String COLUMN_TIME = "time";
10
11 DatabaseManager(Context context) {
12 super(context, DATABASE_NAME, null, DATABASE_VERSION);
13 }
14
15 @Override
16 public void onCreate(SQLiteDatabase sqLiteDatabase) {
17
18
19 String sql = "CREATE TABLE " + TABLE_NAME + " (\n" +
20 " " + COLUMN_ID + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" +
21 " " + COLUMN_DATE + " varchar(200) NOT NULL,\n" +
22 " " + COLUMN_TIME + " INTEGER NOT NULL \n"+
23 ");";
24 sqLiteDatabase.execSQL(sql);
25 }
26
27 @Override
28 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
29 String sql = "DROP TABLE IF EXISTS " + TABLE_NAME + ";";
30 sqLiteDatabase.execSQL(sql);
31 onCreate(sqLiteDatabase);
32 }
33
34
35 boolean addTime(String date, String time) {
36 ContentValues contentValues = new ContentValues();
37 contentValues.put(COLUMN_DATE, date);
38 contentValues.put(COLUMN_TIME, time);
39 SQLiteDatabase db = getWritableDatabase();
40 return db.insert(TABLE_NAME, null, contentValues) != -1;
41 }
42
43 Cursor getTimes() {
44 SQLiteDatabase db = getReadableDatabase();
45 return db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
46 }
47
48 boolean updateTime(String date, String time) {
49 SQLiteDatabase db = getWritableDatabase();
50 ContentValues contentValues = new ContentValues();
51 contentValues.put(COLUMN_DATE, date);
52 contentValues.put(COLUMN_TIME, time);
53 return db.update(TABLE_NAME, contentValues, COLUMN_DATE + "=?", new String[]{String.valueOf(date)}) == 1;
54 }
55
56
57 boolean deleteTime(String date) {
58 SQLiteDatabase db = getWritableDatabase();
59 return db.delete(TABLE_NAME, COLUMN_DATE + "=?", new String[]{String.valueOf(date)}) == 1;
60 }
61}