Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add sub query in SQLITE Android along with 'IN'?

My problem is that I've to write this

select * from contacts where id in (select cpk from mapping where gpk = 'MY-VARIABLE-HERE')

Query in Android but it fails when its run.
When I run this with static variable it works and provide data but when I try this with selections args it fails.

So far I'have tried these:

1) Cursor cursor = myDataBase.rawQuery("select * from contacts where id in (select cpk from mapping where gpk = "+"'"+"?"+"'"+")", new String[]{grpName});

2) Cursor cursor = myDataBase.query(TABLE_CONTACTS, null, "id in", new String[]{"(select cpk from mapping where gpk = '"+grpName+"')"}, null, null, null);

3) Cursor cursor = myDataBase.rawQuery("select * from contacts where id in (select cpk from mapping where gpk = ?)", new String[]{grpName});

Please help me... Thanks in advance...

like image 488
Anuj Sharma Avatar asked Sep 20 '13 09:09

Anuj Sharma


1 Answers

I resolved this, i was passing the wrong value for variable.

Cursor cursor = myDataBase.rawQuery("select * from contacts where id in (select cpk from mapping where gpk = ?)", new String[]{grpName});

Was working correct.

Thanks All for help.. :)

like image 166
Anuj Sharma Avatar answered Sep 28 '22 15:09

Anuj Sharma