I’m fairly new to SQL, and i’m trying to make a query with simple math in the Android app.
Here’s what i’m trying to do –
I have a table named allfoods, with columns named ‘calories’ and ‘size’. Here’s the query-
int tempCalories = 100;
int maxCalories = 300;
Cursor dataFoods = myDataBase.rawQuery("SELECT id FROM allfoods WHERE size <> 0 AND (" + tempCalories + " + (calories * (size / 100)) < " + maxCalories +")" , null);
Will that work?
I have some trouble testing it myself, since the returned values are too varied.
Thanks!
Hello tofira,
Yes, your query will work, if you change the denominator to 100.0 in
(size / 100.0).However, your query might return 0, as your equation:
100 + (calories * (size / 100.0)) < 300will return false when the values for size and/or calories are out of range.
Proof
To test your query I created a table with data, and a tests view.
Data
To have something to test against, I created a table where the values for size ranged in steps of 1 from 1 to 1000 and calories ranged in steps of 5 from 5 to 5000, like this:
Test
To test your equation, I created a view to verify each step of your equation.
SQL
Result
Conclusion
Running your query against my test data would return row 1 to 63.