how to bulk insert in sqlite in android
Use a transaction to insert all the rows -- NOT one row per transaction.
SQLiteDatabase db = ...
db.beginTransaction();
try {
// do ALL your inserts here
db.setTransactionSuccessful()
} finally {
db.endTransaction();
}
EDIT
public void add_cities(List<Cities> list) {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
try {
ContentValues values = new ContentValues();
for (Cities city : list) {
values.put(CityId, city.getCityid());
values.put(CityName, city.getCityName());
db.insert(TABLE_CITY, null, values);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
ALL inserts, ONE transaction.
Thank you @karakuri. i pass the list of city id and city name to add_city function and loop through that list and insert all the data. From this we can insert data to database in a small amount of time.
database class:
public void add_cities(ArrayList<Cities> list) {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
try {
ContentValues values = new ContentValues();
for (Cities city : list) {
values.put(CityId, city.getCityid());
values.put(CityName, city.getCityname());
db.insert(TABLE_CITY, null, values);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
data class:
public class Cities {
public String getCityid() {
return cityid;
}
public void setCityid(String cityid) {
this.cityid = cityid;
}
public String getCityname() {
return cityname;
}
public void setCityname(String cityname) {
this.cityname = cityname;
}
@Expose
private String cityid="0";
@Expose
private String cityname="";
public Cities(){
}
}
activity class:
ArrayList<Cities> mCities;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
db = new DatabaseHandler(getApplicationContext());
executeCircleAndOperatorsList();
}
void executeCircleAndOperatorsList() {
db.ClearTables();
ServiceClient serviceClient = ServiceUtil.getServiceClient();
JsonParser jsonParser = new JsonParser();
mCitiesCallback = new CancelableCallback(citiescallback);
serviceClient.getCities("getCities_v1", mCitiesCallback);
}
Callback<JsonObject> citiescallback=new Callback<JsonObject>() {
@Override
public void success(final JsonObject jsonObject, Response response) {
Thread t=new Thread(new Runnable() {
@Override
public void run() {
parsecities(jsonObject);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(SplashActivity.this,LoginAcivtiy.class);
startActivity(intent);
}
});
}
});
t.start();
}
@Override
public void failure(RetrofitError error) {
}
};
public void parsecities(JsonObject result) {
//Log.v("TAG_RESULT", "" +result.toString());
try{
String Status = result.get("Status").getAsString();
if (TextUtils.equals(Status, "true")) {
Gson gson = new Gson();
JsonArray array = result.get("data")
.getAsJsonArray();
Type type = new TypeToken<ArrayList<Cities>>() {
}.getType();
setmCities((ArrayList<Cities>) gson.fromJson(array, type));
long start = System.currentTimeMillis();
db.add_cities(mCities);
System.out.println(System.currentTimeMillis() - start);
circle_list=db.get_cities();
Log.v("TAG_CIRCLELIST",""+circle_list);
}
else if (TextUtils.equals("Status", "false")) {
// showToast(operators.getMessage());
} else {
throw new JSONException("Something went wrong ");
} }catch(Exception e){
e.printStackTrace();
}
}
public void setmCities(ArrayList<Cities> mCities) {
this.mCities = mCities;
}
}
try SQLiteStatement
in 2-3 times faster than just beginTransaction()
public void add_cities(ArrayList<Cities> list) {
SQLiteDatabase database = this.getWritableDatabase();
String sql = "INSERT INTO " + TABLE_NAME + " VALUES(?, ?)";
SQLiteStatement statement = database.compileStatement(sql);
database.beginTransaction();
try {
for (Cities c : list) {
statement.clearBindings();
statement.bindLong(1, c.getCityId());
statement.bindLong(2, c.getCityName());
statement.execute();
}
database.setTransactionSuccessful();
} finally {
database.endTransaction();
}
}