Query SQLite database using OR

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Query SQLite database using OR

Postby HarryH » Sun Mar 29, 2009 7:36 pm

Hello, I'm new to both Java, Android and programming altogther. I have begun to make my first program and, with the help of these forums, have learnt A LOT. Thanks.
Although I have one thing that I'm stuck on. I would like to query a database by the field "group_number".

I have the following code which returns all the rows where "group_number" = CRITERIA (when CRITERIA = one integer).

Code: Select all
    public Cursor fetchRows() throws SQLException {

 
       Cursor cursor =
       
       mDb.query(true, type, new String[] {"_id",
                       "field_1", "field_2", "example", "group_number"}, "group_number" + "=" + CRITERIA, null,
                        null, null, null, null);
       
return cursor;


However I also have an ArrayList containing serveral integers and I'd like to return rows which have a "group_number" equal to any of the numbers in the ArrayList.

So far all I've done is replace CRITERIA with the name of my ArrayList but it's not working.

Would anybody be kind enough to show me how to do this?

p.s sorry for the bad explanation.

Thanks.
HarryH
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sun Mar 29, 2009 6:50 pm

Top

Postby Alexandro » Mon Mar 30, 2009 7:44 am

Don't know about providing lists in the query, but the simpliest way is to iterate through the list and build a selection string;

Edit:

Here's from the tip of my head. Probably should work, though you might want to edit code a little since it's pretty basic:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. // method to generate filter for group_number
  3.  
  4.     private String generateFilter(ArrayList<Integer> data) {
  5.  
  6.         if (!data.isEmpty()) {
  7.  
  8.                 String filter="group_number = '"+data.get(0)+"'";
  9.  
  10.                 for (int i=1;i<data.size();i++) {
  11.  
  12.                         filter=filter+" OR group_number = '"+data.get(i)+"'";
  13.  
  14.                 }
  15.  
  16.                 return filter;
  17.  
  18.         }
  19.  
  20.         return null;
  21.  
  22.     }
  23.  
  24.  
  25.  
  26.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


And your code with this method:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public Cursor fetchRows() throws SQLException {
  3.  
  4.  
  5.  
  6.  
  7.  
  8.        Cursor cursor =
  9.  
  10.        
  11.  
  12.        mDb.query(true, type, new String[] {"_id",
  13.  
  14.                        "field_1", "field_2", "example", "group_number"}, generateFilter(yourListGoesHere), null,
  15.  
  16.                         null, null, null, null);
  17.  
  18.        
  19.  
  20. return cursor;
  21.  
  22.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
Last edited by Alexandro on Thu Apr 02, 2009 7:15 am, edited 1 time in total.
Alexandro
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Mar 05, 2009 5:42 pm

Postby HarryH » Tue Mar 31, 2009 9:58 pm

Hey, thanks for the quick reply Alaxandro. I haven't tried it yet but I'm sure it'll work.

I'll give it ago when I've got spare minute. Cheers again.
HarryH
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sun Mar 29, 2009 6:50 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 3 guests