Skip to content Skip to sidebar Skip to footer

How To Write A Range Query In Sqlite Android?

Suppose I have a table with 2 fields: Number and Description, I want to get the description from a number I request. For example: I request the number 22. But in the database, ther

Solution 1:

This should ideally work. Will return a cursor with all the results from the table.

db.rawQuery("SELECT Description FROM Table_Name WHERE Num BETWEEN "+(inputNumber-Range)+" AND "+(inputNumber+Range) +"ORDER BY (Num- "+inputNumber+")", null);

Edit: I am glad the above query works for you.But I think this query is the correct answer to the question you specifically asked

db.rawQuery("SELECT Description FROM Table_Name WHERE Num BETWEEN "+(inputNumber-Range)+" AND "+(inputNumber+Range) +"ORDER BY ABS(Num- "+inputNumber+")", null);

Taking the absolute value to avoid negative differences

Solution 2:

Use like this method

publicCursorget_result(){
   Cursor c;
   String sql = "SELECT * FROM " + tableName +"WHERE _id BETWEEN" + value1 +"AND" + value2;
   c = productDatabase.rawQuery(sql, null);
}

Solution 3:

Hi write a query as follows

String query = "SELECT Description FROM Table_Name WHERE Num BETWEEN " + (inputNumber -Range)
            + " AND " + (inputNumber +Range) + "ORDER BY (Price - " + inputNumber + ")";

also you can visit Android SQLite "BETWEEN" not inclusive or returns all records

Post a Comment for "How To Write A Range Query In Sqlite Android?"