views:

57

answers:

2

I am after a code to get the available column names of a table in Android?

I looked around and didn't find anything.

A: 
    try {
        Cursor c = db.query(tableName, null, null, null, null, null, null);
        if (c != null) {
            int num = c.getColumnCount();
            for (int i = 0; i < num; ++i) {
                String colname = c.getColumnName(i);

            }
        }

    } catch (Exception e) {
        Log.v(tableName, e.getMessage(), e);
        e.printStackTrace();
    }
Pentium10
+1  A: 

This is a simpler way

    Cursor ti = db.rawQuery("PRAGMA table_info(mytable)", null);
    if ( ti.moveToFirst() ) {
        do {
            System.out.println("col: " + ti.getString(1));
        } while (ti.moveToNext());
    }
dtmilano
What about getting a content resolvers columns? Is there a simpler way considered to mine, where we run the query on CR?
Pentium10
No, there isn't. In such case a null projection will give you all the columns.
dtmilano