Android Sqlite查询返回null

时间:2012-12-28 19:01:31

标签: android android-sqlite

我在Android中有一个静态sqlite数据库。函数采用int类型的输入并对数据库进行查询。输入值为97,500时工作正常,但如果我输入任何更大的值,则会发生两种情况中的一种

  1. 如果输入为98,000-99,500,则返回null
  2. 如果输入大于100,000,则返回错误的数据
  3. 这是发生故障的功能:

        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;
    }
    

    下面是数据库中数据的截图...我无法弄清楚它为什么不起作用。 Here's a screenshot of the data in database

2 个答案:

答案 0 :(得分:0)

由于我无法看到您的数据库,因此我认为您的问题来源就是这一行:

Cursor cur=db.rawQuery("SELECT * FROM "+BudgetTable+" WHERE "+Low+"<=? AND "+High+">=?", new String[]{String.valueOf(income),String.valueOf(income)});

特别是你使用income来表示大于等于AND小于等于的事实。你确定这是你想要的吗?你确定它不是cuppose

Cursor cur=db.rawQuery("SELECT * FROM "+BudgetTable+" WHERE "+Low+"<=? AND "+High+">=?", new String[]{String.valueOf(lowIncome),String.valueOf(highIncome)});

答案 1 :(得分:0)

您不应将数字作为字符串传递,因为字符串具有不同的比较规则,例如,字符串“2000”将被视为大于字符串“102500”。

他们在javadoc文档中警告过它:

  

selectionArgs您可以在查询中的where子句中包含?s,它将被selectionArgs中的值替换。这些值将绑定为字符串。

您应该像这样重写您的查询:

Cursor cur=db.rawQuery("SELECT * FROM "+BudgetTable+
                      " WHERE "+Low+"<=" + income + " AND "+High+">=" + income);

此外,这是一个非常常见的问题,有些人遇到了类似的问题:SQLite rawQuery selectionArgs and Integers Fields