Get Column names from a table in SQLiteDatabase - Android Database

Android examples for Database:Table Column

Description

Get Column names from a table in SQLiteDatabase

Demo Code


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Main {

  public static List<String> getColumns(SQLiteDatabase db, String tableName) {
    Log.v("List<String>", tableName);
    List<String> ar = null;
    Cursor c = null;/* w  w  w .  j  ava 2s.com*/

    try {
      c = db.rawQuery("SELECT * FROM " + tableName + " LIMIT 1", null);

      if (c != null) {
        ar = new ArrayList<String>(Arrays.asList(c.getColumnNames()));
      }

    } catch (Exception e) {
      Log.v(tableName, e.getMessage(), e);
      e.printStackTrace();
    } finally {
      if (c != null)
        c.close();
    }
    return ar;
  }
}

Related Tutorials