Search code examples
androiddatabasesqliteandroid-sqliteandroid-database

How can I insert Many Rows in Android SQLite


I am making an app using SQLite in Android.

I want to insert 500 rows to my table, but I don't really know how to insert many rows. I tried to insert 10 rows by typing dbInsert() code but typing 500 rows took a lot of codes and made my app slow. These are my codes.

 public WordDBRecord open() throws SQLException {
    dbHelper = new WordDBHelper(context, "WordDB", null, 1);
    database = dbHelper.getWritableDatabase();

    dbInsert("KeyWordDB", "Movie", "Terminator");
    dbInsert("KeyWordDB","Sports","Soccer");
    dbInsert("KeyWordDB","City","BeiJing");
    dbInsert("KeyWordDB","Actor","Tom");
    dbInsert("KeyWordDB","NBA Player","MJ");

    return this;
}
  public void dbInsert(String tableName, String category, String word) {
    Log.d(TAG, "Insert Data ");

    ContentValues contentValues = new ContentValues();
    contentValues.put("CATEGORY", category);
    contentValues.put("WORD", word);

    long id = database.insert(tableName, null, contentValues);
    Log.d(TAG, "id: " + id);
}

I have searched method, but most told me to use ArrayList and for loops. However, as a beginner I don't really understand this method.

So my question is How can I insert 500 rows using for loops or ArrayList?

I will be grateful, if someone answers my question Thank you


Solution

  • try to : INSERT THOUSANDS OF DATA IN SECONDS