Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite query less than or greater than check

I want to use this:

return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, 
KEY_LEVEL }, KEY_LEVEL + ">= 3 AND " + KEY_LEVEL + " < 5", null, null, null, null);

But instead of 5 I want it to check the next column where it finds greater than or equal to three.

So when I give an input of say 5, I want it to check for greater than or equal to 5 in column 3 but less than the value in the next column, column 4.

Original code taken from Sqlite query check - less than and greater than.

like image 289
Ahmed Zafar Avatar asked Mar 22 '23 16:03

Ahmed Zafar


2 Answers

Just do this :

String[] selectionArgs = { "5", "5" };
String[] columns = { KEY_ROWID, KEY_COLUMN3, KEY_COLUMN4 };
String selection = "KEY_COLUMN3 >= ? AND KEY_COLUMN4 < ?";
return mDb.query(DATABASE_TABLE, columns, selection, selectionArgs, null, null, null);
like image 199
Rémi F Avatar answered Mar 27 '23 17:03

Rémi F


For me just passing numerics in String format like @buzeeg offered didn't work.

PROBLEM: My task was to perform UPDATE and in my case it should not have succeed, but as you may guess, when we use 1 as query arg, '1'>2 means true:

UPDATE table SET field = 1 WHERE ? > 2

SOLUTION: CAST helped me to solve it:

SELECT * FROM smth WHERE CAST(? as integer) > 2
like image 23
AinisSK Avatar answered Mar 27 '23 17:03

AinisSK