0

which is the best way to insert 5000 products(name and price) in sqlite? I try not to use 5000 inserts one below the other like that:

 db.execSQL("INSERT INTO " +tableprod+" (id_prod, name) VALUES (7791234567898, 'chocolate')");

Should I use json and gson and don't use sqlite?Thanks.

Community
  • 1
  • 1

1 Answers1

1

Adding one by one would be slow. You can use Batch insert, using which you can insert all the 5000 records in just one transaction. Something like:

// Init SQLiteDatabase
database.beginTransaction();
// do ALL your inserts here
db.setTransactionSuccessful()
db.endTransaction();

For more details see how to bulk insert in sqlite in android and Squeezing Performance from SQLite: Insertions.

Hope this helps.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124