· 9 years ago · Feb 05, 2017, 12:36 AM
1import android.content.ContentValues;
2import android.content.Context;
3import android.database.Cursor;
4import android.database.SQLException;
5import android.database.sqlite.SQLiteDatabase;
6import android.database.sqlite.SQLiteOpenHelper;
7import android.util.Log;
8
9import static com.example.cryptonote.MainActivity.Selected_Database_File_Name;
10
11public class NotesDbAdapter {
12
13public static final String KEY_TITLE = "title";
14public static final String KEY_DATE = "date";
15public static final String KEY_BODY = "body";
16public static final String KEY_ROWID = "_id";
17private static final String TAG = "NotesDbAdapter";
18public DatabaseHelper mDbHelper;
19public SQLiteDatabase mDb;
20
21private static final String DATABASE_CREATE =
22 "create table notes (_id integer primary key autoincrement, "
23 + "title text not null, body text not null, date text not null);";
24
25private static final String DATABASE_NAME = Selected_Database_File_Name;
26private static final String DATABASE_TABLE = "notes";
27private static final int DATABASE_VERSION = 2;
28private final Context mCtx;
29private static class DatabaseHelper extends SQLiteOpenHelper {
30
31 public DatabaseHelper(Context context) {
32 super(context, DATABASE_NAME, null, DATABASE_VERSION);
33 }
34
35 @Override
36 public void onCreate(SQLiteDatabase db) {
37 db.execSQL(DATABASE_CREATE);
38 }
39
40 @Override
41 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
42 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
43 + newVersion + ", which will destroy all old data");
44 db.execSQL("DROP TABLE IF EXISTS notes");
45 onCreate(db);
46 }
47}
48
49public NotesDbAdapter(Context ctx) {
50 this.mCtx = ctx;
51}
52
53public NotesDbAdapter open() throws SQLException {
54 mDbHelper = new DatabaseHelper(mCtx);
55 mDb = mDbHelper.getWritableDatabase();
56 return this;
57}
58
59public void close() {
60 mDbHelper.close();
61}
62
63public long createNote(String title, String body, String date) {
64 ContentValues initialValues = new ContentValues();
65 initialValues.put(KEY_TITLE, title);
66 initialValues.put(KEY_BODY, body);
67 initialValues.put(KEY_DATE, date);
68 return mDb.insert(DATABASE_TABLE, null, initialValues);
69}
70
71public boolean deleteNote(long rowId) {
72 return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
73}
74
75public Cursor fetchAllNotes() {
76 return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
77 KEY_BODY,KEY_DATE}, null, null, null, null, null);
78}
79
80public Cursor fetchNote(long rowId) throws SQLException {
81 Cursor mCursor =
82 mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
83 KEY_TITLE, KEY_BODY,KEY_DATE}, KEY_ROWID + "=" + rowId, null,
84 null, null, null, null);
85 if (mCursor != null) {
86 mCursor.moveToFirst();
87 }
88 return mCursor;
89}
90
91public boolean updateNote(long rowId, String title, String body,String date) {
92 ContentValues args = new ContentValues();
93 args.put(KEY_TITLE, title);
94 args.put(KEY_BODY, body);
95 args.put(KEY_DATE, date);
96 return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
97}
98public boolean isOpen() {
99 return mDb.isOpen();
100}
101}