greendao接入sql和android cursor的简单应用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35506618/article/details/85002350

            String sql="select PAYMENT_PAY_BY,sum(PAYMENT_MONEY) from PAYMENT where SYSTEM_BOOK_CODE = ? " +
                    "and BRANCH_NUM= ?   and  SHIFT_TABLE_NUM=? and SHIFT_TABLE_BIZDAY=?    group by PAYMENT_PAY_BY";
            String[] strings = {systemBookCode,String.valueOf(branchNum),String.valueOf(shiftTableNum),shiftTableBizday};
            Cursor cursor= paymentDao.getDatabase().rawQuery(sql, strings);
            if (cursor.moveToNext()){
                AmountPay amountPay=new AmountPay();
                amountPay.setName(cursor.getString(0));
                amountPay.setAmountMoney(Float.parseFloat(cursor.getString(1)));
                amountPays.add(amountPay);
            }

参考文章:
https://blog.csdn.net/lpf0907/article/details/48444471

https://bbs.csdn.net/topics/392237448

cursor的简单应用

  • Cursor 是每行的集合。

使用 moveToFirst() 定位第一行。

  • 你必须知道每一列的名称。
  • 你必须知道每一列的数据类型。
  • Cursor 是一个随机的数据源。
  • 所有的数据都是通过下标取得。

Cursor 的一些重要方法:

  1. close() 关闭游标,释放资源
  2. copyStringToBuffer(int columnIndex, CharArrayBuffer buffer)
    在缓冲区中检索请求的列的文本,将将其存储
  3. getColumnCount() 返回所有列的总数
  4. getColumnIndex(String columnName) 返回指定列的名称,如果不存在返回-1
  5. getColumnIndexOrThrow(String columnName) 从零开始返回指定列名称,如果不存在将抛IllegalArgumentException 异常。
  6. getColumnName(int columnIndex) 从给定的索引返回列名
  7. getColumnNames() 返回一个字符串数组的列名
  8. getCount() 返回Cursor 中的行数
  9. moveToFirst() 移动光标到第一行
  10. moveToLast() 移动光标到最后一行
  11. moveToNext() 移动光标到下一行
  12. moveToPosition(int position) 移动光标到一个绝对的位置
  13. moveToPrevious() 移动光标到上一行
  14. 访问 Cursor 的下标获得其中的数据
cursor.moveToLast();

last = cursor.getInt(cursor.getColumnIndex("Id"));

Log.i("nowamagicdb", "last_id=>" + last);
  1. 循环 Cursor 取出我们需要的数据

复制代码

if (cursor.getCount() > 0) {

    List<NowaMagic> myList = new ArrayList<NowaMagic>  (cursor.getCount());

    while (cursor.moveToNext()) {

        myList.add(parse(cursor));

    }

    return myList;

}

当cur.moveToNext() 为假时将跳出循环,即 Cursor 数据循环完毕。

如果你喜欢用 for 循环而不想用While 循环可以使用Google 提供的几下方法:

isBeforeFirst() 返回游标是否指向之前第一行的位置
isAfterLast() 返回游标是否指向第最后一行的位置
isClosed() 如果返回 true 即表示该游戏标己关闭
有了以上的方法,可以如此取出数据:

for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext())

{

    int nameColumn = cur.getColumnIndex(People.NAME);

    int phoneColumn = cur.getColumnIndex(People.NUMBER);

    String name = cur.getString(nameColumn);

    String phoneNumber = cur.getString(phoneColumn);

}

猜你喜欢

转载自blog.csdn.net/qq_35506618/article/details/85002350