Does column Exist in SQLiteDatabase Table - Android Database

Android examples for Database:Table Column

Description

Does column Exist in SQLiteDatabase Table

Demo Code

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

public class Main {
  public final static String TAG = "";

  public static boolean columnExist(SQLiteDatabase db, String tableName,
      String columnName) {/*from   w w w .ja  va  2s .  com*/
    boolean result = false;
    Cursor cursor = null;
    try {
      cursor = db.rawQuery("SELECT * FROM " + tableName + " LIMIT 0", null);
      result = cursor != null && cursor.getColumnIndex(columnName) != -1;
    } catch (Exception e) {
      Log.e(TAG, "[columnExist method]error, e: ", e);
    } finally {
      if (null != cursor && !cursor.isClosed()) {
        cursor.close();
      }
    }

    return result;
  }
}

Related Tutorials