AndroidPenguin
AndroidPenguin

Reputation: 3453

Creating a for loop for sql data entry

Currently I have this setup:

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

etc.

And then

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 + ");");

etc.

Is there a way to to create a for loop that runs through 1 to x implementing all? For example I tried:

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

However this didn't work. I then tried setting up:

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

then

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

but that didn't work either.

Sorry if this is a newb question. I have looked everywhere but couldn't find an answer.

Upvotes: 0

Views: 772

Answers (1)

AndroidPenguin
AndroidPenguin

Reputation: 3453

For future reference this is how I implemented it

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();
        }

Upvotes: 1

Related Questions