如何在 Android SQLite 中插入多行
How can I insert Many Rows in Android SQLite
我正在 Android 中使用 SQLite 制作应用程序。
我想在 table 中插入 500 行,但我真的不知道如何插入很多行。
我尝试通过键入 dbInsert() 代码插入 10 行,但键入 500 行需要大量代码并使我的应用程序变慢。
这些是我的代码。
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);
}
我搜索过方法,但大多数告诉我使用 ArrayList 和 for 循环。
但是,作为初学者,我并不真正理解这种方法。
所以我的问题是如何使用 for 循环或 ArrayList 插入 500 行?
如果有人回答我的问题,我将不胜感激
谢谢
我正在 Android 中使用 SQLite 制作应用程序。
我想在 table 中插入 500 行,但我真的不知道如何插入很多行。 我尝试通过键入 dbInsert() 代码插入 10 行,但键入 500 行需要大量代码并使我的应用程序变慢。 这些是我的代码。
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);
}
我搜索过方法,但大多数告诉我使用 ArrayList 和 for 循环。 但是,作为初学者,我并不真正理解这种方法。
所以我的问题是如何使用 for 循环或 ArrayList 插入 500 行?
如果有人回答我的问题,我将不胜感激 谢谢