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.
1 Answer 1
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 aboutInitialize()
?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 statmentif
to make the code less error prone.images = new Images();
where isimages
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 multipleImage
items which it doesn't do.If an exception is thrown from inside the
try
block after the call todao.open();
thedao
will stay open.You should use a
finally
block to close thedao
.
-
\$\begingroup\$ thanks sir for your valuable suggestion i follow your suggestion \$\endgroup\$Garg– Garg2015年08月14日 04:55:36 +00:00Commented 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\$Garg– Garg2015年08月14日 04:58:17 +00:00Commented Aug 14, 2015 at 4:58
-
\$\begingroup\$ Without seeing the code of
IdentifyImageDao
anddatabase.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\$Heslacher– Heslacher2015年08月14日 05:03:03 +00:00Commented 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\$Garg– Garg2015年08月14日 05:27:12 +00:00Commented 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\$Heslacher– Heslacher2015年08月14日 05:30:52 +00:00Commented Aug 14, 2015 at 5:30