-2

Possible Duplicate:
How do I list the tables in a SQLite database file

I am trying to make a simple interface for creating a simple database using Android device. For example, a program for inserting my home collection of movies into database. Maybe I am wrong from the start, but this is my try:

Here I retrieve data from fields and insert them into a new table.

        EditText title = (EditText)findViewById(R.id.Title); 
        String Title = title.getText().toString();

        EditText d = (EditText)findViewById(R.id.director); 
        String Director = d.getText().toString();

                SQLiteDatabase db = openOrCreateDatabase("Films", MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS " + Title + " (Title VARCHAR, Director VARCHAR);");
        db.execSQL("INSERT INTO " + Title + "(Title) VALUES('" + Title + "');");
        db.execSQL("INSERT INTO " + Title + " (Director) VALUES('" + Director + "');");
        db.close();

So for each new Title I create a new table. But then I need all of these tables to print them in a new Activity as a list or something like that. But I don't know how to get all the tables from database.

I found the command:

SQLiteDatabase db = openOrCreateDatabase("Films", MODE_PRIVATE, null);
Cursor c = db.rawQuery("SELECT * FROM tablename", null);

But it works only for one table if I know its name, and what I need is to get all the tables and the data from them.

Could you help me please?

Community
  • 1
  • 1
zeliboba_fett
  • 55
  • 1
  • 3
  • 8

1 Answers1

2

Run a query using this:

Cursor c = db.rawQuery("SELECT * FROM films.sqlite_master WHERE type='table'",null);

EDIT

I forgot that this was Android and rawQuery already has the table name. This should work:

Cursor c = db.rawQuery("SELECT * FROM sqlite_master WHERE type='table'",null);
Barak
  • 16,318
  • 9
  • 52
  • 84
  • Oh, I tried this, but DDMS says "No such table films.sqlite_master", so he thinks it's table name films.sqlite_master. – zeliboba_fett May 13 '12 at 13:41
  • Ok, try `"SELECT * FROM sqlite_master WHERE type='table'"` instead. I forgot that we were talking android specifically. – Barak May 13 '12 at 13:50
  • Thank you very much! Now I'll be thinking of how to sort these tables and how to work with them. – zeliboba_fett May 13 '12 at 14:15
  • So now I selected all the tables, but they all have similar columns, so what's the way I'm gonna retrieve the data from them? – zeliboba_fett May 13 '12 at 14:17
  • Manually go through each table with a SELECT or similar to look at the rows. [Reference](http://stackoverflow.com/questions/82875/how-do-i-list-the-tables-in-a-sqlite-database-file) – Imran Rana May 13 '12 at 14:19
  • As Imran Rana said, run through the tables using SELECT. If you need help with that you should post a separate question, otherwise things will get very confusing. Also, you should mark the answer as accepted (click the check mark). This does two things... 1) It lets everyone know that the issue you asked about has been resolved (another reason not to add on issues) and 2) it gives credit to the person that helped you out. – Barak May 13 '12 at 14:22