Kamalone
Kamalone

Reputation: 4115

Where clause in sqlite in android

I have two application application A and application B. From A I am saving an image, two string values into the database by using contentProvider.

From the application B i am accessing this database and getting the image. The code which i am using is given below

byte b[] = null;
Cursor c = mContext.getContentResolver().query(allTitles, projection,null, null, null);
if (c.moveToFirst()) {
    do{
        if(info.activityInfo.name !=   null) {
            if(c.getString(1).equals(info.activityInfo.name)){
                b=c.getBlob(0);
            }
        }
    } while (c.moveToNext());
}
c.close();

Here in the selection area i am giving null so i am getting all the values in the database and i have to run this loop. Instead of that I want to give a where clause to get the exact raw.

Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name ="+info.activityInfo.name, null, null);

So i have changed like this but it does not worked. How i should give the selection args ? Please help.

Upvotes: 2

Views: 2809

Answers (2)

ngesh
ngesh

Reputation: 13501

try..

Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name = "+info.activityInfo.name, null, null);

or

Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name = ? ", new String[]{info.activityInfo.name}, null);

in case if activity_name is text

Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name = "+"'"+info.activityInfo.name+"'", null, null);

    or

    Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name = ? ", new String[]{"'"+info.activityInfo.name+"'"+}, null);

Upvotes: 3

SurenNihalani
SurenNihalani

Reputation: 1438

You need a '?' for Android to put in values for you in the WHERE clause. The syntax should be,

Cursor c = mContext.getContentResolver().query(allTitles, projection,"activity_name =?", new String[]{info.activityInfo.name}, null);

Upvotes: 0

Related Questions