如何使用for循环插入多个值

时间:2012-07-01 12:47:46

标签: java android sqlite

以下代码适用于DAO和dBhelper类。我有3个表,每个表大约有6列,但我厌倦了“播放”每个方法的字段名称(插入,删除等等),并希望用替换下面的代码for-loop ,浏览字段名称和要插入dB的数据。

所以问题是:
如何将MySQLiteHelper.COLUMN_ID这样的表达式替换为MySQLiteHelper.columnName,以便将此oneliner嵌入到for-loop中。

private String[] allColumns = {
    MySQLiteHelper.COLUMN_level_1,
    MySQLiteHelper.COLUMN_level_2,
    MySQLiteHelper.COLUMN_level_3
};
String[] mydata = myobject.getData();

// Here I would like to use a for-loop for the following lines
values.put(MySQLiteHelper.COLUMN_level_1, mydata[0]);
values.put(MySQLiteHelper.COLUMN_level_2, mydata[1]);
values.put(MySQLiteHelper.COLUMN_level_3, mydata[2]);

1 个答案:

答案 0 :(得分:1)

private String[] allColumns = {
    MySQLiteHelper.COLUMN_level_1,
    MySQLiteHelper.COLUMN_level_2,
    MySQLiteHelper.COLUMN_level_3
};

String[] mydata = myobject.getData();

for(int i = 0; i < allColums.length(); i++){
    values.put(allColums[i], mydata[i]);
}