0

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

  1. 如果输入为 98,000-99,500,则返回 null
  2. 如果输入大于 100,000,则返回错误数据

这是发生故障的功能:

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

下面是数据库中数据的屏幕截图...我不知道为什么它不起作用。 这是数据库中数据的屏幕截图

4

2 回答 2

0

由于我看不到您的数据库,因此我相信您的问题的根源是这一行:

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

特别是您同时使用income大于等于和小于等于的事实。你确定这是你想要的吗?你确定这不是杯具吗?

Cursor cur=db.rawQuery("SELECT * FROM "+BudgetTable+" WHERE "+Low+"<=? AND "+High+">=?", new String[]{String.valueOf(lowIncome),String.valueOf(highIncome)});
于 2012-12-28T19:12:58.077 回答
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

于 2012-12-28T19:16:56.613 回答