47

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?

2
  • 2
    aren't you supposed to know the name of the tables in your database ? Commented Mar 13, 2013 at 11:26
  • 1
    i want to create table dynamically which are not created in database therfor I want to existing table name in my database. like show table in mysql Commented Mar 13, 2013 at 11:30

6 Answers 6

80

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();
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

i don't understand how your query is any different ?
Hello Folks @siddharthLele query will work for iOS Sqlite also, like FMResultSet *s=[database executeQuery:@"SELECT name FROM sqlite_master WHERE type='table'"]; while ([s next]) { NSLog(@"%@",[s stringForColumn:@"name"]); } and iterate by string name. Happy Coding
@Pilpel: I don't see something missing. If you have tested and found something missing, do let me know and I will fix it.
11

Change your sql string to this one:

"SELECT name FROM sqlite_master WHERE type='table' AND name!='android_metadata' order by name"

Comments

8

To get table name with list of all column of that table

    public void getDatabaseStructure(SQLiteDatabase db) {

            Cursor c = db.rawQuery(
                    "SELECT name FROM sqlite_master WHERE type='table'", null);
            ArrayList<String[]> result = new ArrayList<String[]>();
            int i = 0;
            result.add(c.getColumnNames());
            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                String[] temp = new String[c.getColumnCount()];
                for (i = 0; i < temp.length; i++) {
                    temp[i] = c.getString(i);
                    System.out.println("TABLE - "+temp[i]);


                    Cursor c1 = db.rawQuery(
                            "SELECT * FROM "+temp[i], null);
                    c1.moveToFirst();
                    String[] COLUMNS = c1.getColumnNames();
                    for(int j=0;j<COLUMNS.length;j++){
                        c1.move(j);
                        System.out.println("    COLUMN - "+COLUMNS[j]);
                    }
                }
                result.add(temp);
            }
}

Comments

5

Try adding the schema before the table

schema.sqlite_master

From SQL FAQ

If you are running the sqlite3 command-line access program you can type ".tables" to get a list of all tables. Or you can type ".schema" to see the complete database schema including all tables and indices. Either of these commands can be followed by a LIKE pattern that will restrict the tables that are displayed.

From within a C/C++ program (or a script using Tcl/Ruby/Perl/Python bindings) you can get access to table and index names by doing a SELECT on a special table named "SQLITE_MASTER". Every SQLite database has an SQLITE_MASTER table that defines the schema for the database. The SQLITE_MASTER table looks like this:

CREATE TABLE sqlite_master ( type TEXT, name TEXT, tbl_name TEXT, rootpage INTEGER, sql TEXT );

3 Comments

Thanks :( but I have already created schema but still not working Could you paste code of getting all table name from sqlite database android
Try what @Siddharth Lele mentioned. He added a condition because you do not know if you are going to receive any result. If it fails then replace the cursor query: Cursor c = db.rawQuery("SELECT name FROM your_schema.sqlite_master WHERE type='table'", null);
Thanks, but i have already tried with my_schema.sqlite_master but it didn't work. And thanks for your valuable response.
2

Try this:

SELECT name FROM sqlite_master WHERE type = "table";

1 Comment

This seems to be just a fragment of the code in the question. In general, starting with “Try this” suggests that the responder did not try it himself.
0

I tested Siddharth Lele answer with Kotlin and Room, and it works as well.

The same code but using Kotlin and Room is something like that:

val cursor = roomDatabaseInstance.query(SimpleSQLiteQuery("SELECT name FROM sqlite_master WHERE type='table' AND name NOT IN ('android_metadata', 'sqlite_sequence', 'room_master_table')"))
val tableNames = arrayListOf<String>()
if(cursor.moveToFirst()) {
   while (!cursor.isAfterLast) {
      tableNames.add(cursor.getString(0))
      cursor.moveToNext()
   }
}

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.