get Page Sorted By Id With Condition from database table - Android Database

Android examples for Database:SQL Query

Description

get Page Sorted By Id With Condition from database table

Demo Code


//package com.book2s;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Main {
    public static final int PAGE_SIZE = 50;

    public static Cursor getPageSortedByIdWithCondition(SQLiteDatabase db,
            String tableName, String condition, boolean desc, int pageNum) {
        return getPageSortedWithCondition(db, tableName, condition, "id",
                desc, pageNum);/*from  ww w  . jav  a  2  s .c o m*/
    }

    public static Cursor getPageSortedWithCondition(SQLiteDatabase db,
            String tableName, String condition, String keyColumn,
            boolean desc, int pageNum) {
        return db.rawQuery("SELECT * " + "FROM " + tableName + " "
                + "WHERE " + condition + " " + "ORDER BY " + keyColumn
                + " " + (desc ? "DESC" : "") + " " + "LIMIT " + PAGE_SIZE
                + " " + "OFFSET " + (PAGE_SIZE * pageNum) + ";", null);

    }
}

Related Tutorials