1
\$\begingroup\$

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.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 13, 2015 at 13:40
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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.

answered Aug 13, 2015 at 14:30
\$\endgroup\$
7
  • \$\begingroup\$ thanks sir for your valuable suggestion i follow your suggestion \$\endgroup\$ Commented Aug 14, 2015 at 4:55
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Aug 14, 2015 at 4:58
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 14, 2015 at 5:03
  • \$\begingroup\$ 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). \$\endgroup\$ Commented Aug 14, 2015 at 5:27
  • \$\begingroup\$ Then I need to say that without any new code this is as fast as it can get. \$\endgroup\$ Commented Aug 14, 2015 at 5:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.