Get boolean from database using Android and SQLite

后端 未结 10 1544
情话喂你
情话喂你 2020-12-07 09:02

How can I obtain the value of a boolean field in an SQLite database on Android?

I usually use getString(), getInt(), etc. to get the values

10条回答
  •  青春惊慌失措
    2020-12-07 10:02

    Well, that's very simple:

    public boolean getBooleanState(SQLiteDatabase db){
        boolean result = false;
        try{
            String QUERY = "SELECT " + BOOLEAN_DATA + " FROM " + TABLE_NAME + " WHERE " + ID + " = 1";
            Cursor cursor = db.rawQuery(QUERY, null);
            if (cursor.moveToFirst()){
                if(cursor.getString(0).equalsIgnoreCase("1")){
                    result = true;
                }
            }
            c.close();
        }catch(Exception ee){
            Log.e(TAG, "err getBooleanState: " + TABLE_NAME );
        }
        return result;
    }
    

提交回复
热议问题