0

目前我有这个设置:

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 实现所有的 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] + ");");

但这也没有用。

对不起,如果这是一个新手问题。我到处寻找,但找不到答案。

4

1 回答 1

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();
        }
于 2012-11-02T19:45:48.313 回答