Nik Tsekouras
Nik Tsekouras

Reputation: 289

efficient insert in sqlite android

Does anyone know a more efficient way of inserting a row into sqlite? I am worried of split function. Should I store the values in xml or json? If you have any proposals, I would like to know.

my array of values is

private static final String[] inShopCategories = 
     {"Babies;5;@drawable/ic_launcher",
"Fruit / Vegetables;3;@drawable/ic_launcher",
"Meat / Fisth;2;@drawable/ic_launcher"};

and my code is:

 public void onCreate(SQLiteDatabase db){
        //create table
        db.execSQL(cShopCateg);

        //insert default shopping categories with InsertHelper
        InsertHelper ih = new InsertHelper(db, tShopCateg);
        final int nameCol = ih.getColumnIndex(KEY_NAME);
        final int priorityCol = ih.getColumnIndex(KEY_PRIORITY);
        final int iconCol = ih.getColumnIndex(KEY_ICON);

        for(int i=-1,l=inShopCategories.length; ++i<l;){
            ih.prepareForInsert();
            String[] s = inShopCategories[i].split(";");
            ih.bind(nameCol, s[0]);
            ih.bind(priorityCol, s[1]);
            ih.bind(iconCol, s[2]);
            ih.execute();
        }

    }

Upvotes: 0

Views: 542

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006604

do anyone know a more efficient way of inserting row in sqlite?

Yes.

i am worried of split function

That makes sense. You are wasting CPU cycles for no good reason.

should i store the values in xml or json?

Those would waste more CPU cycles for no good reason.

if you have any proposals, i would like to know.

Option #1: Get rid of all this code and pre-package your database, with its data already inserted, with your app, using something like SQLiteAssetHelper.

Option #2: Have your String[] be full SQL INSERT statements and execute those, rather than going through a lot of parsing and concatenating to reach the same end.

Option #3: If somebody is pointing a gun at your head to use InsertHelper, have a two-dimensional String array (String[][]) for your rows and columns.

Upvotes: 3

Related Questions