Android SQLite查询中的限制和偏移量

时间:2017-05-15 14:06:20

标签: android sqlite cursor limit offset

我有一个扩展SQLiteOpenHelper的类。我有一个getCursor()方法。我无法使LIMIT和OFFSET工作。无论我扔什么数字,它总是返回相同的值。我怎样才能使它发挥作用?

public Cursor extraeCursor() {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(contexto);
        String query= "";
        boolean favoritos = pref.getBoolean("favorite",false);

        String offset;
        switch (pref.getString("limit", "1")) {
            case "1":
                offset = Integer.toString(MainActivity.numPage * 1);
                query = "SELECT * FROM lugares limit 1 offset " + offset;
                if (favoritos) query = "SELECT * FROM lugares WHERE favorite = 1 limit 1 offset " + offset;
                break;
            case "2":
                offset = Integer.toString(MainActivity.numPage * 2);
                query= "SELECT * FROM lugares limit 2 offset " + offset;
                if (favoritos) query = "SELECT * FROM lugares WHERE favorite = 1 limit 2 offset " + offset;
                break;
            case "3":
                offset = Integer.toString(MainActivity.numPage * 10);
                query= "SELECT * FROM lugares limit 10 offset " + offset;
                if (favoritos) query = "SELECT * FROM lugares WHERE favorite = 1 limit 10 offset " + offset;
                break;

        }
        SQLiteDatabase bd = getReadableDatabase();
        return bd.rawQuery(query, null);
    }

0 个答案:

没有答案