0

i have this selection query to fetch rows from the sqlite database of android phone contacts.

String selection = ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'"
           + " AND "+ContactsContract.Data.DATA10+ " = '" + select + "'";

Log.d(LOG_TAG,"selection in update data"+selection);

c = mContext.getContentResolver().query(CONTENT_URI, Projection,selection, null, null);

where select = +12244004242

I am successfully able to fetch the reqd data. However my requirement is if i have more than one string. i.e an array of strings.how do i write the query?

I was tring with

String selection = ContactsContract.Data.DATA10 IN ("+select+")";

where select = +12244004242,+12244004245,+12244004248

however i am not getting any values.please help me write the selection query.

1
  • are those Ids you are trying to select? What is the Id field name. Commented Jul 4, 2012 at 8:46

3 Answers 3

1

Try to use IN(" + select + ")

with select = "'+12244004242','+12244004245','+12244004248'"

Sign up to request clarification or add additional context in comments.

2 Comments

I tried that.. i have written in the original problem statement.. but then cursor.getcount() was 0.
Notice that I've added single quotes to the numbers. Perhaps that was what you were missing.
0

You can do a nested OR:

so:

select = +12244004242 OR select=+12244004245 OR select=+12244004248

2 Comments

No, actually i am just giving an example. in my code i dont know the number of elements in the string.i.e i dont know the count of the numbers in the String select.Can I use IN clause??
Well you could iterate through the list and populate with OR statements, or you could look for a more efficient approach than writing the SQL like this, have you tried using the cursor.query method, adding the selection text with "?" symbols and using these in the selectionArgs parameter to populate the fields?
0

You could try using transaction queries, this is how i went about extracting data from my sqlite database, something like:

db = window.openDatabase("DBID", "1.0", "DBName", 50000000);
db.transaction(TableName, errorCB, successCB);
tx.executeSql('SELECT Col1, Col2 FROM TableName WHERE Col1="'+ test +'";', [], querySuccess, errorCB);

function querySuccess(tx, result) 
{
    // DO SOMETHING HERE
}

function errorCB(tx, err) {
    alert("Error processing SQL: "+err.message);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.