-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?

0

1 Answer 1

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

6 Comments

Oh, I tried this, but DDMS says "No such table films.sqlite_master", so he thinks it's table name films.sqlite_master.
Ok, try "SELECT * FROM sqlite_master WHERE type='table'" instead. I forgot that we were talking android specifically.
Thank you very much! Now I'll be thinking of how to sort these tables and how to work with them.
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?
Manually go through each table with a SELECT or similar to look at the rows. Reference
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.