Varshil shah Varshil shah - 6 months ago 7
Android Question

which is way to store 12,000 or above rows locally in Android

I want to store two fields city id and city name and isFavourite locally in android.currently I stored data in SQLite but this is time consuming. So I read other so there is defined shared preference but I don't know is this proper way? I also heard about internal storage and external storage In this fetching data process is faster or not?

Some code that I used for store data:

public void saveCity(String table,int cityid,String cityName,int isfavourite) {
database = this.getWritableDatabase();

ContentValues contentValues = new ContentValues();

contentValues.put(COLUMN_CITYID, cityid);
contentValues.put(COLUMN_CITYNAME, cityName);
contentValues.put(COLUMN_isFavourite, isfavourite);

database.insert(table, null, contentValues);

database.close();
}


I create bus booking application so in this I gave search city option for this purpose I want to stored city persistent

Answer

For Fast Inserting data in your database Use PrepareStatement for it, and for more fast execution make ArrayList for your Data and call method only one for insert all data into the database

public void saveCity(String table, ArrayList<CustomCityClass> cityList) {


        String sql = "INSERT OR REPLACE INTO " + table + " ( "+COLUMN_CITYID +","+ COLUMN_CITYNAME +","+ COLUMN_isFavourite+") VALUES ( ?, ?, ?)";

        mDatabase.beginTransactionNonExclusive();
        SQLiteStatement stmt = mDatabase.compileStatement(sql);

        for (int x = 0; x < cityList.size(); x++) {
            stmt.bindLong(1, cityList.get(x).getCityID());
            stmt.bindString(2, cityList.get(x).getCityname());
            stmt.bindLong(3, cityList.get(x).getisFavourite());

            stmt.execute();
            stmt.clearBindings();

    }
        mDatabase.setTransactionSuccessful();
        mDatabase.endTransaction();


    }

}

and make Custom POJO class CustomCityClass for your city data