# Inserting data into an SQLite database

I've execute this code in Android for inserting 100+ rows in SQLite database. My current approach takes more time to initialize the database.

private void getInitialization() {
try {
dao = new IdentifyImageDao(MainActivity.this);
dao.open();
level = new Level();

for (int i = 0; i < Level.LEVEL_NO_ARRAY.length; i++) {

level.setLevelNo(Level.LEVEL_NO_ARRAY[i]);
level.setTotalGuess(0);
level.setTotalImages(Level.TOTAL_IMAGES_PER_LEVEL[i]);
if (i < 2)
level.setLevelStatus(Level.LEVEL_UNLOCK);
else
level.setLevelStatus(Level.LEVEL_LOCK);
dao.insertLevel(level);
}

images = new Images();
// IMAGES_ID.length = 150
for ( int i = 0; i < Images.IMAGES_ID.length; i++ ) {
images.setLogoId(Images.IMAGES_ID[i]);
images.setLogoCheck(Images.IMAGE_UNCHECK);
dao.insertImages(images);
}
dao.close();
} catch (Exception e) {

}
}


Database method for image insertion

public long insertImages(Images images) throws SQLException {
ContentValues value = new ContentValues();
value.put(LOGO_NO, images.getLogoId());
value.put(LOGO_CHEECK, images.getLogoCheck());
return database.insert(TABLE_LOGO_MASTER, null, value);
}


Please review my code and suggest a more efficient and faster approach for inserting multiple data.

void getInitialization()

• The name of the method implies that we would get something but the return value is void. Maybe a better name would be in order. How about Initialize() ?

• an empty catch block is always a very bad sign. If you want to swallow any exception, you should clearly state with a comment why you are swallowing the exceptions.

• it is always recommended to use braces {} for single statment if to make the code less error prone.

• images = new Images(); where is images declared ? It would be better to have this as a method scoped variable instead of reusing a class level one.

In addition the name Images implies that it contains multiple Image items which it doesn't do.

• If an exception is thrown from inside the try block after the call to dao.open(); the dao will stay open.

You should use a finally block to close the dao.

• thanks sir for your valuable suggestion i follow your suggestion – Garg's Aug 14 '15 at 4:55
• please guide me more for improve data inserting speed in database my current logic take more time for inserting data. What there are any faster approach for same task then i increase my code efficiency – Garg's Aug 14 '15 at 4:58
• Without seeing the code of IdentifyImageDao and database.insert() it is hard to tell how to speed things up. If you think about editing your question to inc these details make sure to not invalidate the givven answer, otherwise the edit will be rolled back. – Heslacher Aug 14 '15 at 5:03
• sir in code i have declare insertImage() method for insert image id and there lock status in database. This method insert image one by one in database, it's calling in getInitialization() with in loop and loop iterate till length of Array (containing images id). – Garg's Aug 14 '15 at 5:27
• Then I need to say that without any new code this is as fast as it can get. – Heslacher Aug 14 '15 at 5:30