where语句使用不正确的值

时间:2011-03-05 20:23:01

标签: android sqlite

祝福所有人。我正在尝试迭代一系列值来从数据库填充列表视图。据我所知,我正在使用的代码应该正常工作。由于某种原因,select语句使用的是另一个值而不是我告诉它的值。

这是在我的适配器中我应该能够输入“1”(save_slot)和“91”(项目)来检索value1和值2.

 public Cursor fetchItem(String saveslot, String item) throws SQLException {

        Cursor mCursor =

            mDb.query(true, DATABASE_PUSHERS_TABLE, new String[] {KEY_PROWID,
                     SAVE_SLOT,ITEM,VALUE1,VALUE2}
            ,SAVE_SLOT + " = " + saveslot + " AND "+ITEM + " = " + item, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

        }   

出于某种原因,在执行select statment时,它使用适当的value1而不是item。所以它正在寻找save_slot“1”,但它没有查找项目“91”,而是查看value1“91”的值,即“EMPTY SLOT”。所以它正在寻找savelot = 1和item = EMPTY SLOT,这是不存在的。

logcat的:

DEBUG/ITEM CHECK: SAVESLOT #: 0
DEBUG/ITEM CHECK: ITEMSLOT #: 0
DEBUG/ITEM CHECK: ITEM #: 91
DEBUG/ITEM CHECK: ITEMSLOT #: 1
DEBUG/ITEM CHECK: ITEM #: 99
DEBUG/ITEM CHECK: ITEMSLOT #: 2
DEBUG/ITEM CHECK: ITEM #: 98
DEBUG/ITEM CHECK: ITEMSLOT #: 3
DEBUG/ITEM CHECK: ITEM #: 97
DEBUG/ITEM CHECK: ITEMSLOT #: 4
DEBUG/ITEM CHECK: ITEM #: 96
DEBUG/ITEM CHECK: ITEMSLOT #: 5
DEBUG/ITEM CHECK: ITEM #: 95
DEBUG/ITEM CHECK: ITEMSLOT #: 6
DEBUG/ITEM CHECK: ITEM #: 94
DEBUG/ITEM CHECK: ITEMSLOT #: 7
DEBUG/ITEM CHECK: ITEM #: 93
DEBUG/ITEM CHECK: ITEMSLOT #: 8
DEBUG/ITEM CHECK: ITEM #: 100
DEBUG/ITEM CHECK: ITEMSLOT #: 9
DEBUG/ITEM CHECK: ITEM #: 90
DEBUG/ITEM CHECK: SAVESLOT #: 1
DEBUG/ITEM CHECK: ITEMSLOT #: 0
                            @this should be item 91 NOT "EMPTY SLOT"
@DEBUG/ITEM CHECK: ITEM #: EMPTY SLOT
DEBUG/AndroidRuntime: Shutting down VM
WARN/dalvikvm(8300): threadid=3: thread exiting with uncaught exception (group=0x4001b390)
ERROR/AndroidRuntime(8300): Uncaught handler: thread main exiting due to uncaught exception
ERROR/AndroidRuntime(8300): java.lang.RuntimeException: Unable to resume activity   {circle.pusherb/circle.pusherb.SaveScreen}:     android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
ERROR/AndroidRuntime(8300): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
ERROR/AndroidRuntime(8300):     at circle.pusherb.SaveScreen.fillData(SaveScreen.java:77)

CODE:

  private void fillData() {
        Cursor note = null;
        String[] from = InfoItemList;
        int SaveSlot = 1;
        int x = 0;
        for (SaveSlot = 0;SaveSlot < 3;SaveSlot++ ){
          Log.d("ITEM CHECK: SAVESLOT #", Integer.toString(SaveSlot));
        for ( x = 0; x<(InfoItemList.length); x++){

            note = mDbHelper.fetchItem(Integer.toString(SaveSlot),InfoItemList[x]);
            startManagingCursor(note);
            Log.d("ITEM CHECK: ITEMSLOT #", Integer.toString(x));
            Log.d("ITEM CHECK: ITEM #", InfoItemList[x]);

       from[x] = note.getString(
                note.getColumnIndexOrThrow(pusherDbAdapter.VALUE1));

        }
        }


        int[] to = new int[]{R.id.text1, R.id.text2, R.id.text3,
                R.id.text5,R.id.text6,R.id.text7,R.id.text8,R.id.text9
                ,R.id.text10};


        SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.pusherlist_row, null, from, to);
        setListAdapter(notes);
    }

//InfoItemList[] contains numbers 90 to 100 except 92.
//EMPTY SLOT is currently the value returned when a record containing item 91 and save_slot 1-3.

1 个答案:

答案 0 :(得分:1)

尝试此查询:

mDb.query(true, DATABASE_PUSHERS_TABLE, new String[] {KEY_PROWID, SAVE_SLOT, 
           ITEM,VALUE1,VALUE2}, 
        SAVE_SLOT + " = ? AND "+ITEM + " = ?" ,
        new String[] {saveslot, item}, null, null, null, null);