· 9 years ago · Nov 10, 2016, 04:46 AM
1package edu.miami.urcnumbersloot.databasenotes;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.database.sqlite.SQLiteOpenHelper;
8//=============================================================================
9public class DataSQLiteDB {
10 //-----------------------------------------------------------------------------
11 public static final String DATABASE_NAME = "ListOfNotes.db";
12 private static final int DATABASE_VERSION = 2;
13
14 private static final String NOTE_TABLE_NAME = "NoteList";
15 private static final String CREATE_NOTE_TABLE =
16 "CREATE TABLE IF NOT EXISTS " + NOTE_TABLE_NAME +
17 "(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
18 "noteData TEXT NOT NULL," +
19 "date TEXT NOT NULL" +
20 ");";
21
22 private DatabaseHelper dbHelper;
23 private SQLiteDatabase theDB;
24 //-----------------------------------------------------------------------------
25 public DataSQLiteDB(Context theContext) {
26
27 dbHelper = new DatabaseHelper(theContext);
28 theDB = dbHelper.getWritableDatabase();
29 }
30 //-----------------------------------------------------------------------------
31 public void close() {
32 dbHelper.close();
33 theDB.close();
34 }
35
36 public boolean deleteNote(long noteId) {
37
38 return(theDB.delete(NOTE_TABLE_NAME,"_id =" + noteId,
39 null) > 0);
40 }
41 //-----------------------------------------------------------------------------
42 public boolean addNote(ContentValues noteData) {
43 return(theDB.insert(NOTE_TABLE_NAME,null,noteData) >= 0);
44 }
45
46 //-----------------------------------------------------------------------------
47 public Cursor fetchAllNotes() {
48 String[] fieldNames = {"noteData","date", "_id"};
49 return(theDB.query(NOTE_TABLE_NAME,fieldNames,null,null,
50 null,null,"_id"));//last arg is "order by"
51 }
52 //-----------------------------------------------------------------------------
53 private ContentValues noteDataFromCursor(Cursor cursor) {
54
55 String[] fieldNames;
56 int index;
57 ContentValues noteData;
58
59 if (cursor != null && cursor.moveToFirst()) {
60 fieldNames = cursor.getColumnNames();
61 noteData = new ContentValues();
62 for (index=0;index < fieldNames.length;index++) {
63 if (fieldNames[index].equals("noteData")) {
64 noteData.put("noteData", cursor.getString(index));
65 } else if (fieldNames[index].equals("_id")) {
66 noteData.put("_id", cursor.getInt(index));
67 } else if (fieldNames[index].equals("date")) {
68 noteData.put("date",cursor.getString(index));
69 }
70 }
71 return(noteData);
72 } else {
73 return(null);
74 }
75 }
76 //=============================================================================
77 private static class DatabaseHelper extends SQLiteOpenHelper {
78 //-------------------------------------------------------------------------
79 public DatabaseHelper(Context context) {
80
81 super(context,DATABASE_NAME,null,DATABASE_VERSION);
82 }
83 //-------------------------------------------------------------------------
84 public void onCreate(SQLiteDatabase db) {
85
86 db.execSQL(CREATE_NOTE_TABLE);
87 }
88 //-------------------------------------------------------------------------
89 @Override
90 public void onOpen(SQLiteDatabase db) {
91
92 super.onOpen(db);
93 }
94 //-------------------------------------------------------------------------
95 public void onUpgrade(SQLiteDatabase db,int oldVersion,
96 int newVersion) {
97
98 db.execSQL("DROP TABLE IF EXISTS " + NOTE_TABLE_NAME);
99 onCreate(db);
100 }
101 //-------------------------------------------------------------------------
102 }
103//=============================================================================
104}
105//=============================================================================