While working with SQLiteCursor in Android i came to know that the getColumnIndex() is behaving case sensitive for example:
Example:
The best and recommended approach using SQLite is that you declare all your table name and column name static
, final
and class
level.. for example:
// write table name
public static final String TABLE_MESSAGE = "messages";
// and column name accordingly
public static final String COLUMN_ID = "_id";
public static final String COLUMN_MESSAGE = "message";
so the benefit of this approach is you don't need to remember the spelling and case etc of the table and column names.
when you access any table or column you simply use these static variables for example:
// TABLE creation sql statement
private static final String TABLE_CREATE = "create table "
+ TABLE_MESSAGE + "( " + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_MESSAGE
+ " text not null);";
while querying:
database.query(TABLE_MESSAGE, new String[]{COLUMN_ID,COLUMN_MESSAGE}, null, null, null, null, null);
or it may be used in Cursor
int index = cursor.getColumnIndex(COLUMN_MESSAGE);
this will help you to avoid such conflicts of case sensitivity and spelling mistakes. :)