Get Column Name by table name - Android Database

Android examples for Database:Table Column

Description

Get Column Name by table name

Demo Code

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

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

public class Main {
  public static List<String> GetColumns(SQLiteDatabase db, String tableName) {
    List<String> ar = null;
    Cursor c = null;//from w w  w  .  j a  v  a 2s .c o m
    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