继承自上面两篇的文章。本篇主要介绍分页查询和查询的总记录数:
方法为:
/** * * * @param pageSize * 每一页数据的大小 比如20个数据 * @param pageNum * 多少页 比如 第一页 第二页 * @return */ public List<Account> queryPage(int pageSize,int pageNum) { String index = (pageNum - 1) * pageSize + ""; // 索引 翻页时的起始索引 String count = pageSize + ""; // 查询多少条数据 List<Account> list = new ArrayList<Account>(); sqliteDatabase db = helper.getReadableDatabase(); Cursor c = db.rawQuery("SELECT * FROM account LIMIT ?,?",new String[] { index,count });// 查询表中所有的数据 while (c.moveToNext()) { int id = c.getInt(c.getColumnIndex("_id")); String name = c.getString(c.getColumnIndex("name")); int balance = c.getInt(c.getColumnIndex("balance")); list.add(new Account(id,name,balance)); } c.close(); db.close(); return list; } /** * * 查询总共有多少条数据 * * @return */ public int queryCount() { sqliteDatabase db = helper.getReadableDatabase(); Cursor c = db.rawQuery("SELECT COUNT(*) FROM account",null); c.moveToNext(); int count = c.getInt(0); c.close(); db.close(); return count; }
测试的方法为:
public void testQueryPage(){ AccountDao dao = new AccountDao(getContext()); List<Account> list = dao.queryPage(20,1); for (Account account : list) { System.out.println(account); } } public void testQueryCount(){ AccountDao dao = new AccountDao(getContext()); System.out.println(dao.queryCount()); }原文链接:https://www.f2er.com/sqlite/200353.html