I have a static sqlite database in Android. A function takes a int type of input and makes a query to the database. It's working fine upto input values of 97,500, but if I enter anything larger then one of two cases happens
Here's the function that's malfunctioning:
Budget getBudget(int income,String name)
{
Budget B=null;
Log.d("DB", String.valueOf(income));
try{
SQLiteDatabase db=this.getReadableDatabase();
Cursor cur=db.rawQuery("SELECT * FROM "+BudgetTable+" WHERE "+Low+"<=? AND "+High+">=?", new String[]{String.valueOf(income),String.valueOf(income)});
Log.d("DB", String.valueOf(cur.getCount()));
if(cur.getCount()!=0)
{
Log.d("DB", "Cursor not empty");
cur.moveToFirst();
B=new Budget(0, income,cur.getInt(cur.getColumnIndex(MortgageRent)),cur.getInt(cur.getColumnIndex(Utilities)) ,
cur.getInt(cur.getColumnIndex(LightnPower)), cur.getInt(cur.getColumnIndex(PhonenInternet)),
cur.getInt(cur.getColumnIndex(HomeMaintenance)), cur.getInt(cur.getColumnIndex(HomeCleaning)),
cur.getInt(cur.getColumnIndex(Groceries)), cur.getInt(cur.getColumnIndex(Clothing)),0,
cur.getInt(cur.getColumnIndex(PersonalGrooming)), cur.getInt(cur.getColumnIndex(MedicalnPharmacy)),
cur.getInt(cur.getColumnIndex(HealthInsurance)), cur.getInt(cur.getColumnIndex(LifeInsurance)),
cur.getInt(cur.getColumnIndex(HomeInsurance)), cur.getInt(cur.getColumnIndex(Accounting)),
cur.getInt(cur.getColumnIndex(BankFees)), cur.getInt(cur.getColumnIndex(Fuel)),
cur.getInt(cur.getColumnIndex(ServicenRepairs)), cur.getInt(cur.getColumnIndex(GovernmentCharges)),
cur.getInt(cur.getColumnIndex(CarInsurance)),0, cur.getInt(cur.getColumnIndex(PublicTransport)),
cur.getInt(cur.getColumnIndex(Entertainment)), cur.getInt(cur.getColumnIndex(SportsnGym)),
cur.getInt(cur.getColumnIndex(EatOut)), cur.getInt(cur.getColumnIndex(Alcohol)),
cur.getInt(cur.getColumnIndex(Gifts)), cur.getInt(cur.getColumnIndex(Holidays)),
cur.getInt(cur.getColumnIndex(NewspapernMagazine)), cur.getInt(cur.getColumnIndex(Others)), 0, 0, name);
Log.d("DB", String.valueOf(cur.getInt(cur.getColumnIndex(IncomeLevel))));
cur.close();
db.close();
}
}catch(Exception ex)
{
Log.d("DB", ex.getMessage());
}
return B;
}
Below is a screenshot of the data in database...I can't figure out why it doesn't work.
As I can't see your database, I am led to believe the source of your problem is this line:
Cursor cur=db.rawQuery("SELECT * FROM "+BudgetTable+" WHERE "+Low+"<=? AND "+High+">=?", new String[]{String.valueOf(income),String.valueOf(income)});
specifically the fact that you are using income
for both greater-equal than AND less than-equal. Are you sure this is what you want? Are you sure it is not cuppose to be
Cursor cur=db.rawQuery("SELECT * FROM "+BudgetTable+" WHERE "+Low+"<=? AND "+High+">=?", new String[]{String.valueOf(lowIncome),String.valueOf(highIncome)});
You shouldn't pass numbers as strings, because strings have different rules of comparison, for example, the string "2000" will be considered greater than the string "102500".
They warn about it in the javadoc documentation:
selectionArgs You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.
Your should rewrite your query like this:
Cursor cur=db.rawQuery("SELECT * FROM "+BudgetTable+
" WHERE "+Low+"<=" + income + " AND "+High+">=" + income);
Also, it is quite a common issue and some people encountered a similar problem: SQLite rawQuery selectionArgs and Integers Fields
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.