I have a method to insert values into sqlite
in a class. And in the main activity i am parsing json values and trying to insert the values into sqlite
in a loop as i have got to insert more than one row. But only one row is getting added into database.
Method to add multiple rows is as below:
public void addSession(String sessionname,String start_time,String end_time,String id) {
SQLiteDatabase db = this.getWritableDatabase();
try {
db.beginTransaction();
String sql = "Insert into session (id, sessionname,start_time, end_time) values(?,?,?,?)";
SQLiteStatement insert = db.compileStatement(sql);
insert.bindString(1, id);
insert.bindString(2, sessionname);
insert.bindString(3, start_time);
insert.bindString(4, end_time);
insert.execute();
db.setTransactionSuccessful();
} catch (Exception e) {
Log.w("Appet8:",e );
} finally {
db.endTransaction();
}
}
In the below for loop i am trying to add values into sqlite table:
JSONObject jsonObject = new JSONObject(response);
JSONArray foodsessions = jsonObject.getJSONArray("foodsessions");
for(int i=0;i<foodsessions.length();i++) {
JSONObject session_object = foodsessions.getJSONObject(i);
String session = session_object.getString("sessionname");
String start_time = session_object.getString("start_time");
String end_time = session_object.getString("end_time");
String session_id = session_object.getString("id");
db.addSession(session,start_time,end_time,session_id);
}