· 7 years ago · Nov 22, 2018, 12:18 PM
1package com.example.opilane.launchposts;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8import android.support.annotation.Nullable;
9
10public class DbAdapter {
11
12 public static final String TABLE_NAME = "posts";
13 public static final int COLUMN_ID = 0;
14 public static final int COLUMN_TIME = 1;
15 public static final int COLUMN_POST = 2;
16 public static final String[] TABLE_COLUMNS = new String[]{
17 "id", "time", "post"
18 };
19 private static final String DBFILENAME = "posts.db";
20 private static final int DBVERSION = 1;
21 private static final String INITIAL_SCHEMA = "create table posts " +
22 "(id integer primary key autoincrement, time varchar(19)" +
23 "not null, post varchar(100) not null)";
24
25 private final Context context;
26 private DatabaseHelper dbHelper;
27 private SQLiteDatabase db;
28
29 public DbAdapter(Context context){
30 this.context = context;
31 dbHelper = new DatabaseHelper(context);
32 }
33
34 public DbAdapter open(){
35 db = dbHelper.getWritableDatabase();
36 return this;
37 }
38
39 public void close() {dbHelper.close();}
40
41 public long insert(String time, String post){
42 ContentValues values = new ContentValues();
43 values.put(TABLE_COLUMNS[COLUMN_TIME], time);
44 values.put(TABLE_COLUMNS[COLUMN_POST], post);
45 return db.insert(TABLE_NAME, null, values);
46 }
47
48 public boolean update(String time, String post){
49 ContentValues values = new ContentValues();
50 values.put(TABLE_COLUMNS[COLUMN_TIME], time);
51 values.put(TABLE_COLUMNS[COLUMN_POST], post);
52 return db.update(TABLE_NAME, values, "id = 1",
53 null) != 0;
54 }
55
56 public boolean delete(int id){
57 return db.delete(TABLE_NAME, "id = ?", new String[]
58 {"" + id}) != 0;
59 }
60
61 public void delete(){db.delete(TABLE_NAME, null, null);}
62
63 public Cursor getRows(){
64 Cursor cursor = db.query(TABLE_NAME, TABLE_COLUMNS, null,
65 null, null, null, null);
66 return cursor;
67 }
68 public Cursor getRow(int id){
69 Cursor cursor = db.query(TABLE_NAME, TABLE_COLUMNS,
70 "ID = ?", new String[] {
71 "" + id}, null, null,
72 null, null);
73 return cursor;
74 }
75
76 public Cursor getRows(String date){
77 Cursor cursor = db.query(TABLE_NAME, TABLE_COLUMNS,
78 "time like ?", new String[]
79 {date + "%"}, null, null,
80 null, null);
81 return cursor;
82 }
83
84 private static class DatabaseHelper extends SQLiteOpenHelper{
85
86 DatabaseHelper(Context context) {
87 super(context, DBFILENAME, null, DBVERSION);
88 }
89
90
91
92 @Override
93 public void onCreate(SQLiteDatabase db) {
94 db.execSQL(INITIAL_SCHEMA);
95
96 }
97
98 @Override
99 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
100 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
101 onCreate(db);
102 }
103
104 @Override
105 public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
106 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
107 onCreate(db);
108 }
109 }
110}