Skip to content

Instantly share code, notes, and snippets.

@robertlevonyan
Last active August 2, 2018 11:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertlevonyan/a21763f36cd34c6e08dcc2a13cc02e71 to your computer and use it in GitHub Desktop.
Save robertlevonyan/a21763f36cd34c6e08dcc2a13cc02e71 to your computer and use it in GitHub Desktop.
SQLite Model class
// A helper class to work with SQLite database
public class DatabaseHelper extends SQLiteOpenHelper {
// Database version, which should be upgraded each time we made changes in database structure
private static final int DATABASE_VERSION = 1;
// Database name
private static final String DATABASE_NAME = "notes_db";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating tables
@Override
public void onCreate(SQLiteDatabase db) {
// create notes table
db.execSQL(Note.CREATE_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop (or delete) older table if existed
db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
// Create tables again
onCreate(db);
}
public long insertNote(String note) {
// get writable database as we want to write data
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
// `id` and `timestamp` will be inserted automatically.
// no need to add them
values.put(Note.COLUMN_NOTE, note);
// insert row
long id = db.insert(Note.TABLE_NAME, null, values);
// close db connection
db.close();
// return newly inserted row id
return id;
}
public Note getNote(long id) {
// get readable database as we are not inserting anything
SQLiteDatabase db = this.getReadableDatabase();
// creating a Cursor object to be able to lookup the table
Cursor cursor = db.query(Note.TABLE_NAME,
new String[]{Note.COLUMN_ID, Note.COLUMN_NOTE, Note.COLUMN_TIMESTAMP},
Note.COLUMN_ID + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
// prepare note object
Note note = new Note(
cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)),
cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)),
cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
// close the db connection
cursor.close();
return note;
}
public List<Note> getAllNotes() {
List<Note> notes = new ArrayList<>();
// query to select all the data from the notes table
String selectQuery = "SELECT * FROM " + Note.TABLE_NAME + " ORDER BY " +
Note.COLUMN_TIMESTAMP + " DESC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Note note = new Note();
note.setId(cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)));
note.setNote(cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)));
note.setTimestamp(cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
notes.add(note);
} while (cursor.moveToNext());
}
// close db connection
db.close();
// return notes list
return notes;
}
public int getNotesCount() {
String countQuery = "SELECT * FROM " + Note.TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
// return count
return count;
}
public int updateNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Note.COLUMN_NOTE, note.getNote());
// updating row
int row = db.update(Note.TABLE_NAME, values, Note.COLUMN_ID + " = ?",
new String[]{String.valueOf(note.getId())});
db.close();
return row;
}
public void deleteNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Note.TABLE_NAME, Note.COLUMN_ID + " = ?",
new String[]{String.valueOf(note.getId())});
db.close();
}
}
public class Note {
// The name of table which will be created in the database
public static final String TABLE_NAME = "notes";
// The names of columns which will be in our table
public static final String COLUMN_ID = "id";
public static final String COLUMN_NOTE = "note";
public static final String COLUMN_TIMESTAMP = "timestamp";
// Class members of model according to table
private int id;
private String note;
private String timestamp;
// An SQL query to create a table with given columns
public static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," // PRIMARY KEY for uniqueness and AUTOINCREMENT to grow value each time by 1
+ COLUMN_NOTE + " TEXT,"
+ COLUMN_TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP" // take current time
+ ")";
// A primary constructor
public Note() {
}
// A constructor with parameters
public Note(int id, String note, String timestamp) {
this.id = id;
this.note = note;
this.timestamp = timestamp;
}
// Getters and setters
public int getId() {
return id;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getTimestamp() {
return timestamp;
}
public void setId(int id) {
this.id = id;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment