· 8 years ago · May 07, 2018, 05:10 PM
1package id.webdroid.catatanku.database;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8
9import java.util.ArrayList;
10import java.util.List;
11
12import id.webdroid.catatanku.database.model.Note;
13
14public class DatabaseHelper extends SQLiteOpenHelper {
15
16 // Database Version
17 private static final int DATABASE_VERSION = 1;
18
19 // Database Name
20 private static final String DATABASE_NAME = "notes_db";
21
22
23 public DatabaseHelper(Context context) {
24 super(context, DATABASE_NAME, null, DATABASE_VERSION);
25 }
26
27 // Creating Tables
28 @Override
29 public void onCreate(SQLiteDatabase db) {
30
31 // create notes table
32 db.execSQL(Note.CREATE_TABLE);
33 }
34
35 // Upgrading database
36 @Override
37 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
38 // Drop older table if existed
39 db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
40
41 // Create tables again
42 onCreate(db);
43 }
44
45 public long insertNote(String note) {
46 // get writable database as we want to write data
47 SQLiteDatabase db = this.getWritableDatabase();
48
49 ContentValues values = new ContentValues();
50 // `id` and `timestamp` will be inserted automatically.
51 // no need to add them
52 values.put(Note.COLUMN_NOTE, note);
53
54 // insert row
55 long id = db.insert(Note.TABLE_NAME, null, values);
56
57 // close db connection
58 db.close();
59
60 // return newly inserted row id
61 return id;
62 }
63
64 public Note getNote(long id) {
65 // get readable database as we are not inserting anything
66 SQLiteDatabase db = this.getReadableDatabase();
67
68 Cursor cursor = db.query(Note.TABLE_NAME,
69 new String[]{Note.COLUMN_ID, Note.COLUMN_NOTE, Note.COLUMN_TIMESTAMP},
70 Note.COLUMN_ID + "=?",
71 new String[]{String.valueOf(id)}, null, null, null, null);
72
73 if (cursor != null)
74 cursor.moveToFirst();
75
76 // prepare note object
77 Note note = new Note(
78 cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)),
79 cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)),
80 cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
81
82 // close the db connection
83 cursor.close();
84
85 return note;
86 }
87
88 public List<Note> getAllNotes() {
89 List<Note> notes = new ArrayList<>();
90
91 // Select All Query
92 String selectQuery = "SELECT * FROM " + Note.TABLE_NAME + " ORDER BY " +
93 Note.COLUMN_TIMESTAMP + " DESC";
94
95 SQLiteDatabase db = this.getWritableDatabase();
96 Cursor cursor = db.rawQuery(selectQuery, null);
97
98 // looping through all rows and adding to list
99 if (cursor.moveToFirst()) {
100 do {
101 Note note = new Note();
102 note.setId(cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)));
103 note.setNote(cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)));
104 note.setTimestamp(cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
105
106 notes.add(note);
107 } while (cursor.moveToNext());
108 }
109
110 // close db connection
111 db.close();
112
113 // return notes list
114 return notes;
115 }
116
117 public int getNotesCount() {
118 String countQuery = "SELECT * FROM " + Note.TABLE_NAME;
119 SQLiteDatabase db = this.getReadableDatabase();
120 Cursor cursor = db.rawQuery(countQuery, null);
121
122 int count = cursor.getCount();
123 cursor.close();
124
125
126 // return count
127 return count;
128 }
129
130 public int updateNote(Note note) {
131 SQLiteDatabase db = this.getWritableDatabase();
132
133 ContentValues values = new ContentValues();
134 values.put(Note.COLUMN_NOTE, note.getNote());
135
136 // updating row
137 return db.update(Note.TABLE_NAME, values, Note.COLUMN_ID + " = ?",
138 new String[]{String.valueOf(note.getId())});
139 }
140
141 public void deleteNote(Note note) {
142 SQLiteDatabase db = this.getWritableDatabase();
143 db.delete(Note.TABLE_NAME, Note.COLUMN_ID + " = ?",
144 new String[]{String.valueOf(note.getId())});
145 db.close();
146 }
147}