How to get all table names in android sqlite database?

后端 未结 5 1602
逝去的感伤
逝去的感伤 2020-12-05 06:37

I have tried this code

Cursor c=db.rawQuery("SELECT name FROM sqlite_master WHERE type = \'table\'",null);
c.moveToFirst();
while(!c.isAfterLast()){         


        
5条回答
  •  长情又很酷
    2020-12-05 07:17

    Checked, tested and functioning. Try this code:

    Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
    
    if (c.moveToFirst()) {
        while ( !c.isAfterLast() ) {
            Toast.makeText(activityName.this, "Table Name=> "+c.getString(0), Toast.LENGTH_LONG).show();
            c.moveToNext();
        }
    }
    

    I am assuming, at some point down the line, you will to grab a list of the table names to display in perhaps a ListView or something. Not just show a Toast.

    Untested code. Just what came at the top of my mind. Do test before using it in a production app. ;-)

    In that event, consider the following changes to the code posted above:

    ArrayList arrTblNames = new ArrayList();
    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();
            }
        }
    

提交回复
热议问题