Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

java - How to get all table names in android sqlite database?

I have tried this code

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

But it throws the error:

"android.database.sqlite.SQLiteException: no such table: sqlite_master(code 1):, while 
compiling: SELECT name FROM sqlite_master WHERE type='table'"

How to fetch all the table names?

question from:https://stackoverflow.com/questions/15383847/how-to-get-all-table-names-in-android-sqlite-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...