为sql数据输入创建for循环

时间:2012-09-17 02:03:01

标签: sqlite loops for-loop

目前我有这个设置:

String string1 = ....
String string2 = ....
String string3 = ....
String string4 = ....

然后

db.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (" + string1 + ");");
db.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (" + string2 + ");");
db.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (" + string3 + ");");
db.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (" + string4 + ");");

有没有办法创建一个贯穿1到x实现all的for循环?例如,我试过:

for (int number = 0; number <= 4; number++) { 
    String s = "string" + number; 
    db.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (" + s + ");");

然而这不起作用。然后我尝试着设置:

String x[] = {string1, string2, string3, string4}`

然后

for (int number = 0; number <= 4; number++) { 
     String s = "string" + number; 
     db.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES (" + x[number] + ");");

但这也不起作用。

很抱歉,如果这是一个新问题。我到处寻找但找不到答案。

1 个答案:

答案 0 :(得分:1)

为了将来参考,这是我实施它的方式

String[] sqlString = {string1, string2, string3...etc.}

db.beginTransaction();

        try {
            for (int n = 1; n < sqlString.length; n++) {

                db.execSQL("INSERT INTO " + DATABASE_TABLE + " VALUES ("
                        + sqlString[n] + ");");

            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }