Teja Nandamuri
Teja Nandamuri

Reputation: 11201

How to get table rows from sqlite_master using tbl_name

I am trying to get a table based on the rootpage number/tbl_name and store in a custom TableObject.

I am able to get the table name by the root page number/tbl_name, but I have no idea about getting the row values from that table.

Here is what I have:

SELECT * FROM sqlite_master WHERE name='test1';

or

SELECT * FROM sqlite_master WHERE rootpage=4;

This thing gives me a row which has a table . enter image description here

Now how can I get the test1 contents ?

The test1 table have two rows in it, called age and name. How can I access those columns ?

At the end I end up using the cursor, so the query will be something like:

 cursor=db.rawQuery("SELECT * FROM sqlite_master WHERE rootpage = '" + rootNumber + "'",null);

I can use

cursor.getString(getColumnIndex("name"))      //w.r.t sqlite_master

cursor.getString(getColumnIndex("rootpage"))  //w.r.t sqlite_master

How can I use the same cursor to get something like:

cursor.getString(getColumnIndex("age"))      //w.r.t test1
cursor.getString(getColumnIndex("name"))      //w.r.t test1

to get the values from the resulted table.

Upvotes: 1

Views: 1075

Answers (1)

Haresh Chaudhary
Haresh Chaudhary

Reputation: 4400

I would suggest you that once you query sqlite_master table using cursor A, don't use the same cursor A as it contains result of the that query result.

Hence you need to fire another query and store the next result in another cursor B so that you don't lose the data from your first query.

For example, for master table,you do something like :

Cursor cursorA = db.rawQuery("SELECT * FROM sqlite_master WHERE rootpage = '" + rootNumber + "'",null);
cursorA.moveToFirst();
String tableName = cursorA.getString(getColumnIndex("name"));

For further getting result from the table "test1" that you got result from the previous query, you can do something like this :

Cursor cursorB = db.rawQuery("SELECT * FROM '" + tableName + "'",null);
cursorB.moveToFirst();
String name= cursorB.getString(getColumnIndex("name")) ; 
int age= cursorB.getString(getColumnIndex("age")) ;

Hence you will get the result that you need further from table "test1".

Hope this helps you :)

Upvotes: 1

Related Questions