Search code examples
androiddatabaseandroid-sqliteandroid-query

how to extract data in sqlite DB in a particular range


I am writing an android application in which i have to get a range of values like the below,

I have a DB in which the contents are like the below 5 columns,

_ID | _NAME | _AGE | _HEIGHT | _WEIGHT |

1   | aabv  | 10   | 5.0     | 30

1   | babs  | 10   | 5.1     | 28

1   | cabg  | 10   | 5.2     | 29

1   | dabs  | 10   | 5.3     | 32

1   | eaba  | 10   | 5.0     | 31

1   | gabe  | 10   | 5.0     | 31

1   | iabr  | 10   | 5.0     | 31

1   | xabt  | 10   | 5.0     | 31

1   | zabu  | 10   | 5.0     | 31

now i would like to extract the rows which are 'd' to 's' => output should => dabs, eaba, gabe, iabr

Can i do it so? if so what is the sql query.

I tried the below - "request the name which are in between D and S"

mCursor = mDb.query(DATABASE_NAME, new String[] { _ID, _NAME, _AGE, _HEIGHT, _WEIGHT }, 
                _NAME + " BETWEEN " + "D" + " AND " + "S", 
                null,
                null, 
                null, 
                _NAME + " ASC");

Its giving an error saying there is an error in the statement =>

_NAME + " BETWEEN " + "A" + " AND " + "S"

Kindly help.

[1/11] Q: In the above table if i want to extract multiple data from a column say, i would like to extract where _HEIGHT = (5.1,5.2,5.3) How i should add the query?


Solution

  • As they are text and not numbers, you need to encapsulate them in quotes in your query string.

    _NAME + " BETWEEN " + "'A'" + " AND " + "'S'"