Retrieving column names with column values in sqlite using cursor

PrachiN picture PrachiN · Apr 19, 2014 · Viewed 11.8k times · Source

I want to display column names with column values in the Listview using cursor. My present code shows only the column values.

public void openAndQueryDatabase() {

    db = openOrCreateDatabase( "mydatabase.db", SQLiteDatabase.CREATE_IF_NECESSARY , null ); 

    Cursor cursor = db.rawQuery("select * from "+ table + " where name='"+ name + "'",  null);


    int count = cursor.getColumnCount();

    if (cursor!=null )
    { 

        if  (cursor.moveToFirst())
        {
            do 
            {
                 for (int i =0 ; i< count; i++)
                {
                String data = cursor.getString(i);
                details.add(data);
                }
            } 
            while (cursor.moveToNext()); 
        } 

         }
}

Answer

Gabe Sechan picture Gabe Sechan · Apr 19, 2014

Use getColumnName(int columnIndex) to get the name of the row for a given index.