Paste data into a database - what is the best way to do it
I suggest you create your own object that will represent your table, where the properties of the object will be equal to the columns in the table, eq
public class Contact { private String name; private String number; private String image; private boolean conn;
Now your approach sounds like a "spaghetti code." There is no need to have four ArrayLists, and this design is inefficient and correct.
You will now have one ArrayList object to contact with 500 children.
What is the best way to insert?
Surely wrap your insert in one TRANSACTION , which will speed up your insertions quickly and most importantly, your database business becomes much more secure, and then you do not need to worry about the possibility of losing the integrity of the database.
Transaction example (one of my personal projects):
public boolean insertTransaction(int count) throws SQLException { boolean result = false; try { db = openWrite(DataSource.getInstance(mContext)); ContentValues values = new ContentValues(); if (db != null) { db.beginTransaction(); for (int i = 0; i < count; i++) { values.put(SQLConstants.KEY_TYPE, "type" + i); values.put(SQLConstants.KEY_DATE, new Date().toString()); db.insertOrThrow(SQLConstants.TEST_TABLE_NAME, SQLConstants.KEY_TYPE, values); values.clear(); } db.setTransactionSuccessful(); result = true; } return result; } finally { if (db != null) { db.endTransaction(); } close(db); } }
source share