I'm practicing android, I was able to insert using db.insert()
without the use of getWritableDatabase()
why is that? I thought we need db = getWritableDatabase();
before we can insert to the database
private SQLiteDatabase db;
@Override
public void onCreate(SQLiteDatabase db) {
this.db = db;
//some table creation
db.execSQL(TABLE);
fillQuestionsTable();
}
private void insertQuestion(Question question){
ContentValues cv = new ContentValues();
//some code
db.insert(QuestionsTable.TABLE_NAME, null, cv);
}
I thought it should be like this?
private void insertQuestion(Question question){
db = getWritableDatabase();
ContentValues cv = new ContentValues();
//some code
db.insert(QuestionsTable.TABLE_NAME, null, cv);
}
here's my implementation:
private void fillQuestionsTable(){
Question q1 = new Question("Programming, Easy: A is correct", "A", "B", "C",
1, Question.DIFFICULTY_EASY, Category.PROGRAMMING);
insertQuestion(q1);
}