SQL数据库事务的使用及分页查询

1、SQL数据库事务的使用(批量添加或删除使用)

           db.beginTransaction();
            /**
             * 执行sql语句   db.execSQL(sql);
             */
            db.setTransactionSuccessful();
            db.endTransaction();

2、分页查询

思路就是计算显示多少页
1、先获取数据库中的总条目

/**
  * 获取数据的总条目
  * @param type 根据类型去查询数据
  * @return 总条目
  */
 public int getDataCount(String type) {
     int count = 0;
     db = helper.getWritableDatabase();
     Cursor cursor = null;
     if (type.equals("全部")) {
       //查询全部表中数据的总条目
         sql = "select * from account";
         cursor = db.rawQuery(sql,null);
     }else {
         sql = "select * from account where type = ?";
         cursor = db.rawQuery(sql, new String[]{type});
     }
     if (cursor != null) {
         count = cursor.getCount();      //获取数据总条目
     }
     return count;
 }

2、给定每页显示多少条数据‘

 //accountSize表示的是每页显示的数目  我这里给定的是10

3、根据上面个条件算出总页码

	//countSum是调用getDataCount得到的总条目,,accountNum总页数
   accountNum = (int)Math.ceil(countSum/(double)accountSize);  //根据总条目与每页展示数据条目   获得总页数

1、示例代码

 private void initData() {
    final DBDAO dbdao = new DBDAO(getContext());     //数据库操作类
    if(type!=null) {
        countSum = dbdao.getDataCount(type);     //获取数据表的总条目
        accountNum = (int)Math.ceil(countSum/(double)accountSize);  //根据总条目与每页展示数据条目   获得总页数
        if(currentAccount == 1){		//如果是第一页
        //currentAccount表示的是页数,accountSize每一页显示多少条数据
            mCostBeans = dbdao.selectCostBean(type,currentAccount,accountSize);     //获取到数据源
        }
        final LsvAccountAdapter adapter = new LsvAccountAdapter(mCostBeans, getContext());    //列表适配器
        lsvAccount.setAdapter(adapter);
        //ListView滑动监听
        lsvAccount.setOnScrollListener(new AbsListView.OnScrollListener() {
      /**
         *监听着ListView的滑动状态改变。官方的有三种状态SCROLL_STATE_TOUCH_SCROLL、
         *SCROLL_STATE_FLING、SCROLL_STATE_IDLE:
         * SCROLL_STATE_TOUCH_SCROLL:手指正拖着ListView滑动
         * SCROLL_STATE_FLING:ListView正自由滑动
         * SCROLL_STATE_IDLE:ListView滑动后静止
          */
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                    //isDivPage判断是否分页 
                    if(isDivPage && AbsListView.OnScrollListener.SCROLL_STATE_IDLE == scrollState){
                        if(currentAccount < accountNum){
                            currentAccount ++;
                            //根据最新的页码加载获取集合到存储数据源中
                            mCostBeans.addAll(dbdao.selectCostBean(type,currentAccount,accountSize));
                            adapter.notifyDataSetChanged(); //刷新
                        }
                    }
            }
			/**
           * firstVisibleItem: 表示在屏幕中第一条显示的数据在adapter中的位置
           * visibleItemCount:则表示屏幕中最后一条数据在adapter中的数据,
           * totalItemCount则是在adapter中的总条数
           * */
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                isDivPage = ((firstVisibleItem + visibleItemCount) == totalItemCount);
            }
        });
    }

``

selectCostBean代码

/**
 * 根据当前页码查询获取该页码对应的数据
 * @param type           查询的表数据类型
 * @param currentAccount 当前页码
 * @param accountSize    显示多少条数据
 * @return 当前对应数据集合
 */
@Override
public ArrayList<CostBean> selectCostBean(String type, int currentAccount, int accountSize) {
    ArrayList<CostBean> mBeans = new ArrayList<>();
    CostBean bean = null;
    int index = (currentAccount - 1) * accountSize;   //获取当前页码第一条数据
    db = helper.getWritableDatabase();
    Cursor cursor;
    if (!type.equals("全部")) {
        sql = "select * from account where type = ? limit ?,?";
        cursor = db.rawQuery(sql, new String[]{type, index + "", accountSize + ""});
    } else {
        sql = "select * from account limit ?,?";
        cursor = db.rawQuery(sql, new String[]{index + "", accountSize + ""});
    }
    if (cursor != null) {
        if (cursor.moveToLast()) {        //是否最后一条记录
            do {
                bean = new CostBean();
                bean.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
                bean.setTime(cursor.getString(cursor.getColumnIndex("time")));
                bean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                bean.setDescription(cursor.getString(cursor.getColumnIndex("description")));
                bean.setType(cursor.getString(cursor.getColumnIndex("type")));
                bean.setMoney(cursor.getFloat(cursor.getColumnIndex("money")));
                bean.setImagePath(cursor.getString(cursor.getColumnIndex("imagePath")));
                mBeans.add(bean);
            } while (cursor.moveToPrevious());       //移动到上一条
            cursor.close();
        }
    }
    db.close();
    return mBeans;
}

   



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xpq_lrh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值