sqlite查询,分页查询

private static final String TABLENAME = "mytab";
 private SQLiteDatabase db = null;

 public Qu(SQLiteDatabase db) {
  this.db = db;
 }

 public List<String> find() {
  List<String> all = new ArrayList<String>();
  String sql = "select id,name,birthday from mytab";
   Cursor result = db.rawQuery(sql, null);

                     从第一个开始查            不是最后一个            移动到下一个
   for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext())
   {
   all.add(result.getInt(0)+result.getString(1)+result.getString(2));
   }
   db.close();

return all;

}

  List<String> all = new ArrayList<String>();

 String[] da ={"id","name","birthday"};
  Cursor result = db.query(TABLENAME, da, null, null, null, null,null);
   for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext())
   {
   all.add(result.getInt(0)+result.getString(1)+result.getString(2));
   }
   db.close();

return all;

 

 String sql = "select id,name,birthday from mytab where name like ? or birthday like ?";
  String keyword = "5";
  String[] args = new String[] { "%" + keyword + "%", "%" + keyword + "%" };
  Cursor result = db.rawQuery(sql, args);
  for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()) {
   all.add(result.getInt(0) + result.getString(1)
     + result.getString(2));
  }
  return all;

public List<String> select() {
        List<String> all = new ArrayList<String>();

        String sql = "select * from user where user_name like ? limit ?,?";
        int group = 2;//分的页数,下标从0开始,因此我们想得到第二页得要-1
        int size = 5;//每页显示的个数
        String[] srgs = {"%d%",String.valueOf((group-1)*size),String.valueOf(size)};
        Cursor cu = db.rawQuery(sql, srgs);
        while(cu.moveToNext()){
            all.add(cu.getInt(0)+cu.getString(1)+cu.getString(2));
        }
        db.close();
        return all;

    }

 

posted @ 2015-10-27 18:53  aaddrrooiidd  阅读(521)  评论(0编辑  收藏  举报