Android SQLite SELECT WHERE不起作用

时间:2015-04-16 18:32:35

标签: android sqlite

我的表名是DATABASE_TABLE2。我在该表中有5列。现在我想说的是 SELECT * DATABASE_TABLE2 REV_FACILITY ="厕所"

我想在数组列表视图中显示..这是我尝试过的代码,它正在崩溃。我知道问题出在数据库方法上,但我不知道该怎么做

private void displayListView() {


        Cursor cursor = dbHelper.getAllToilets();

        startManagingCursor(cursor);

        // The desired columns to be bound
        String[] columns = new String[]{

                SQL.KEY_REV_STATION_NAME,
                SQL.KEY_DATE,
                SQL.KEY_REV_FACILITY,
                SQL.KEY_RATING,
                SQL.KEY_COMMENT
        };

        // the XML defined views which the data will be bound to
        int[] to = new int[]{
                R.id.SRevName,
                R.id.SRevDate,
                R.id.SRevFacility,
                R.id.SRateBar,
                R.id.SRevComment,

        };

        // create the adapter using the cursor pointing to the desired data
        //as well as the layout information
        final SimpleCursorAdapter myCursorAdapter =
                new SimpleCursorAdapter(
                        this, R.layout.review_list,
                        cursor,
                        columns,
                        to);

        myCursorAdapter.setDropDownViewResource(android.R.layout.simple_selectable_list_item);

        ListView listView = (ListView) findViewById(R.id.ToiletReviews);
        // Assign adapter to ListView
        listView.setAdapter(myCursorAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {


            }
        });

    }
    public Cursor getAllToilets() {
        //String where = null;
        String where = KEY_REV_FACILITY = "Toilets";
        Cursor c = db.query(true, DATABASE_TABLE2, ALL_REV_KEYS,
                where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

2 个答案:

答案 0 :(得分:1)

看起来像这样:

String where = KEY_REV_FACILITY = "Toilets";

您首先将"Toilets"分配给KEY_REV_FACILITY,然后分配给where

也许你的意思是:

String where = KEY_REV_FACILITY + "= Toilets";

注意:如果您要比较SQLite数据库中的字符串,我建议您使用 SELECT LIKE 。看看这个SO answer

答案 1 :(得分:0)

改变这个:

   public Cursor getAllToilets() {
        //String where = null;
        String where = KEY_REV_FACILITY = "Toilets";
        Cursor c = db.query(true, DATABASE_TABLE2, ALL_REV_KEYS,
                where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

对此:

public Cursor getAllToilets() {        
        String where = KEY_REV_FACILITY + "=?";
        String[] selectionArgs=new String[] {"Toilets"}
        Cursor c = db.query(DATABASE_TABLE2, columns,
                where, selectionArgs,null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }