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