3

Is it possible to query the list of tables in a database into a Cursor? I don't know how to implement the show tables method in android. One way is to store all tables in a "collector" table as records but this is a workaround.

P.S: I don't need all the tables just the ones starting with "PR_" (I know how to handle this in queries, what I don't know is if that matters in this case).

erdomester
  • 11,789
  • 32
  • 132
  • 234

2 Answers2

7

I found the solution. In the SQLiteOpenHelper class:

public Cursor showAllTables(){
        String mySql = " SELECT name FROM sqlite_master " + " WHERE type='table'             "
                + "   AND name LIKE 'PR_%' ";
        return ourDatabase.rawQuery(mySql, null);
    }

In the activity:

     Cursor c = info.showAllTables();
             if (c.moveToFirst())
             {
             do{
                todoItems.add(c.getString(0));

                }while (c.moveToNext());
             }
             if (todoItems.size() >= 0)
             {
                 for (int i=0; i<todoItems.size(); i++)
                 {
                     Log.d("TODOItems(" + i + ")", todoItems.get(i) + "");

                 }

             }
erdomester
  • 11,789
  • 32
  • 132
  • 234
2

In SQLite you should be able to query SQLITE_MASTER. Not tested this on Android though. See http://sqlite.org/faq.html#q7

Alex Florescu
  • 5,096
  • 1
  • 28
  • 49