I have the DB below and when I try to run an SQL command it doesn’t do quite what it should do (the code below should have a cursorSize of 1 but it shows 0).
The code I run is:
SQLiteDatabase db = this.getReadableDatabase();
String countQuery = "SELECT songid FROM TABLE_TRACKS WHERE (tagid = 7 OR tagid = 10) AND tagid = 9";
Cursor cursor = db.rawQuery(countQuery, null);
int cursorSize = cursor.getCount();
Its something to do with the where clause, because when I change the brackets and make it
WHERE (tagid=9 AND tagid = 7) OR tagid=10
It works because int cursorSize is 2.
Is there something silly I am missing out or something I don’t know about SQL WHERE clause or AND/OR operators?
Would really appreciate if someone could explain this for me and how to get the results that I want. (Below I want to return one row with songid equal to 3.)
Ther is nothing wrong with the result. It should give you zero because:
You need to check your app logic and what are you trying to accomplish here.
Edit:
So think of songIDs as songs and tagIDs as Tags (guitar, piano, romantic and so on) so if I want to find the songs which has (piano AND guitar) OR romantic tag how is it possible ? do I have to change my db (table) or can I reformulate my sql ?If this is the context of your app, and you expect some input combinations from the users, I suggest you change your db scheme. try to add Boolean columns for each tag. This will be easier in case you are expecting such combinations and it will not differ that much in terms of space.