jeudi 25 février 2016

Multiple execSQL() in one function

I wrote a scheduler app which adds schedule data to a database when the user updates the schedule from the internet using this:

sqLiteDatabase.execSQL("INSERT OR IGNORE INTO `subject` (`id`, `date`, `start`, `end`, `name`, `room`, `teacher`, `class_id`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", new String[]{id, date, start, end, name, room, teacher, classId});

This is working fine.

However there could be a scenario, that the online schedule will change it's content. Therefore I would write the function like this:

sqLiteDatabase.execSQL("DELETE FROM `subject` WHERE `date` = ? AND `class_id` = ?", new String[] {date, classId});
sqLiteDatabase.execSQL("INSERT OR IGNORE INTO `subject` (`id`, `date`, `start`, `end`, `name`, `room`, `teacher`, `class_id`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", new String[]{id, date, start, end, name, room, teacher, classId});

The data is deleted fine, but now not all the content is added back in the database. So what I think:

The delete query is still running while the second insert query is executed too

So, my question is. How can I wait until the deletion is finished to add the new data back properly?

Aucun commentaire:

Enregistrer un commentaire