Android - SQLite Cursor getColumnIndex() is case sensitive?

后端 未结 4 2010
孤街浪徒
孤街浪徒 2020-12-06 06:29

While working with SQLiteCursor in Android i came to know that the getColumnIndex() is behaving case sensitive for example:

Example:

4条回答
  •  攒了一身酷
    2020-12-06 07:15

    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. :)

提交回复
热议问题