Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

android - Check if value exist in DB with Boolean in Java

What I want to do is check if there's any data inside the profilePicture column on 'profiles' table, if there's no data, the getCount will return 0 and shall return false. How do I make the query to only fetch the data in profilePicture column to see if it's null or not for the user id.

public Boolean checkProfilePicture(String user_id){
        SQLiteDatabase MyDB = this.getReadableDatabase();

        Cursor cursor = MyDB.rawQuery("Select * from profiles where ID = ? and profilePicture = NULL", new String[] {user_id});
        System.out.println(cursor.getCount());

        if (cursor.getCount() > 0)
            return true;
        else
            return false;
    }
question from:https://stackoverflow.com/questions/65869309/check-if-value-exist-in-db-with-boolean-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your query should return only 1 column which is a boolean value in the form of 1 (true) or 0 (false) and for this you must use an expression like:

profilePicture IS NOT NULL

You must compare to NULL with the operator IS or IS NOT and not with = or <> or !=:

public boolean checkProfilePicture(String user_id){
    SQLiteDatabase MyDB = this.getReadableDatabase();
    Cursor cursor = MyDB.rawQuery("SELECT profilePicture IS NOT NULL FROM profiles WHERE ID = ?", new String[] {user_id});
    boolean result = cursor.moveToFirst();
    if (result) result = (cursor.getInt(0) == 1) ;
    cursor.close();
    MyDB.close();
    return result;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...