2

How to get all table was i created in SQlite database to string array?.please give me suggestion. Thanks in advance My database class as below

private static class DBHelper extends SQLiteOpenHelper {

   /* public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }*/
   public DBHelper(Context context) {
       super(context, DB_NAME, null, DB_VIRSION);
       // TODO Auto-generated constructor stub
   }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "+DB_TABLE+" ("+
                        KEY_ROWID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
                        KEY_NAME+" TEXT NOT NULL, "+
                        KEY_MOBILE+" NUMBER NOT NULL, "+
                        KEY_DATE+" TEXT NOT NULL);"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE);
    }
}
public MyDatabase(Context c){
    ourContext=c;
}
public MyDatabase open(){
    ourHelper=new DBHelper(ourContext);
    ourDatabase=ourHelper.getWritableDatabase();
    return this;
}
public void close(){
    ourHelper.close();
}

2 Answers 2

4

Try this code sample

ArrayList<String> arrTblNames = new ArrayList<String>();
SqlHelper sqlHelper = new SqlHelper(this, "TK.db", null, 1);
SQLiteDatabase DB = sqlHelper.getWritableDatabase();
Cursor c = DB.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);

        if (c.moveToFirst()) {
            while ( !c.isAfterLast() ) {
                arrTblNames.add( c.getString( c.getColumnIndex("name")) );
                c.moveToNext();
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

0

I solve question like this code

public ArrayList<String> getAllTable() {

    ArrayList<String> arrTblNames = new ArrayList<String>();
    Cursor c = ourDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
    c.moveToFirst();
    while (!c.isAfterLast()) {

        arrTblNames.add(c.getString(c.getColumnIndex("name")));
        c.moveToNext();
    }
    // make sure to close the cursor
    c.close();
    return arrTblNames;
}

3 Comments

how is it different from my answer other than returning the list in the method
Your Answer is write but i can't understand this line in your answer SqlHelper sqlHelper = new SqlHelper(this, "TK.db", null, 1); , Thanks for your replay. and Both code sucess for my question
give a checkmark then

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.