Creating the database
Create a new class called TimeTrackerOpenHelper that extends SQLiteOpenHelper. Pass the database name and the database version to super.
TimeTrackerOpenHelper(Context context)
super(context, "timetracker.db", null, 1)
// timetracker.db is the db name, 1 is version
public void onCreate(SQLiteDatabase database) {
database.execSQL(
"create table timerecords " +
"(id integer primary key, time text, notes text)");
);
}
public void onUpgrade(SQLiteDatabase database) {
database.execSQL("DROP TABLE IF EXISTS timerecords");
onCreate(database);
}
instead of using execSQL, we can use ContentValues
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_NAME, VALUE);
database.insert(TABLE_NAME, null, contentValues);
query the database
sqlite queries return cursors
public Cursor getAllTimeRecords() {
return database.rawQuery(
"select * from " + TABLE_NAME, null
);
}
public class TimeTrackerAdapter extends CursorAdapter {
public TimeTrackerAdapter(Context context, Cursor cursor) {
super(content, cursor);
}
}