0

Please review the following piece of code:

try {
          db.beginTransaction();
          db.execSQL(DBConstants.PRAG_FOR_KEYS_ON);

          db.execSQL(DBConstants._BLDG_CREATE);
          db.execSQL(DBConstants._BLDG_INDEX);
          for(int x = 0; x < 28; x = x+1){
              db.execSQL(DBConstants._BLDG_INSERT+x);
          }

          db.execSQL(DBConstants.PRAG_FOR_KEYS_OFF);
          db.setTransactionSuccessful();
        } catch (SQLException e) {
          e.printStackTrace();
        }finally{
          db.endTransaction();
        }

Each of the insert constants (representing a row of new data) are numbered thus:

public static final String _BLDG_INSERT0 = "<SQL insert statement>"

...all the way up to 28 ("_BLDG_INSERT28").

Is there ANY way i can execute these SQL statements in a for loop? If i can, how do i concactenate the number on to the name of the constant AND have it recognized by the java interpreter in the correct way?

Thanks!

2
  • Make sure your autoincrement primary key starts and ends with your x values..Than its possible using Cursors Commented Jan 16, 2013 at 11:23
  • You can using reflection. Isn't to much complicated, wait a moment and I will show an example. Commented Jan 16, 2013 at 11:30

3 Answers 3

1

It's not clear from the question whether you are able to change the constants. If you can, it would be better if you could put the statements in an array.

String[] _BLDG_INSERT = {"<SQL insert statement>",   // 0
                         "<SQL insert statement>",   // 1
                         ...
                         "<SQL insert statement>"    // 28
                        };

And then you can just access them like this.

for(int x = 0; x < 28; x = x+1){
    db.execSQL(DBConstants._BLDG_INSERT[x]);
}

Or better still:

for(String s : DBConstants._BLDG_INSERT) {
    db.execSQL(s);
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's BRILLIANT...i should've thought of the array thing...couldn't see the array forest for the class trees (grin). THANKS!
0
    public ArrayList<String> getAllRecord()
{
    ArrayList<String> total = new ArrayList<String>();
    Cursor cursor1 = null;
    try
    {
        cursor1 = getDBobject().rawQuery(
                "select * from "
                + Your table name + ";", null);
        if (cursor1.moveToFirst())
        {
            do
            {
                String cid = cursor1.getString(1);
                total.add(cid);

            }
            while (cursor1.moveToNext());
        }
    }
    catch (Exception e)
    {
        System.out.println("" + TAG + " :" + e);
    }
    finally
    {
        if (cursor1 != null && !cursor1.isClosed())
        {
            cursor1.close();
        }
    }
    return total;
}

This will return you all the datas according to your insertion order

Comments

0

Try something like this:

public class testeReflection {
    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException{
        MyClass myClass = new MyClass();

        Class aClass = MyClass.class;

        for(int i = 0; i < 5; i++){
            try {
                Field field = aClass.getField("VAR" + i);

                String s = (String)field.get(myClass);

                System.out.println("myClass[" + i + "] = " + s);                
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(testeReflection.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(testeReflection.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static class MyClass{
        public static final String VAR0 = "VARIAVEL 01";
        public static final String VAR1 = "VARIAVEL 02";
        public static final String VAR2 = "VARIAVEL 03";
        public static final String VAR3 = "VARIAVEL 04";
        public static final String VAR4 = "VARIAVEL 05";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.