首页>代码>安卓家庭理财通客户端实例>/work/app/src/main/java/com/example/a80205050/myapplication/Dao/InaccountDao.java
package com.example.a80205050.myapplication.Dao;

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

import java.util.ArrayList;
import java.util.List;

import com.example.a80205050.myapplication.Modle.Tb_inaccount;

/**
 * Created by 80205050 on 2017/10/30.
 */

public class InaccountDao {
    private DBOpenHelper helper;//创建DBOpenHelper对象
    private SQLiteDatabase db;//创建SQLiteDataBase对象
    public InaccountDao(Context context) {
        helper=new DBOpenHelper(context);
    }
    /**
     * 添加收入信息
     * @param tb_inaccount
     */
    public void add(Tb_inaccount tb_inaccount){
        db=helper.getWritableDatabase();//初始化SQLiteDatabase
        db.execSQL("insert into  tb_inaccount(_id,money,time,type,handler,mark) values(?,?,?,?,?,?)",
                new Object[]{tb_inaccount.get_id(),tb_inaccount.getMoney(),tb_inaccount.getTime(),
                tb_inaccount.getType(),tb_inaccount.getHandler(),tb_inaccount.getMark()});
    }

    /**
     * 更新数据
     * @param tb_inaccount
     */
    public void update(Tb_inaccount tb_inaccount){
        db=helper.getWritableDatabase();//初始化SQLiteDatabase
        db.execSQL("update tb_inaccount set money=?,time=?,type=?,handler=?,mark=? where _id=?",
                new Object[]{tb_inaccount.getMoney(),tb_inaccount.getTime(),
                tb_inaccount.getType(),tb_inaccount.getHandler(),tb_inaccount.getMark(),tb_inaccount.get_id()});
    }

    /**
     * 根据id查找收入的数据
     * @param id
     * @return
     */
    public Tb_inaccount find(int id){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select _id,money,time,type,handler,mark from tb_inaccount " +
                "where _id=?",new String[]{String.valueOf(id)});
        if(cursor.moveToNext()){
            return new Tb_inaccount(cursor.getInt(cursor.getColumnIndex("_id")),
                                    cursor.getDouble(cursor.getColumnIndex("money")),
                                    cursor.getString(cursor.getColumnIndex("time")),
                                    cursor.getString(cursor.getColumnIndex("type")),
                                    cursor.getString(cursor.getColumnIndex("handler")),
                                    cursor.getString(cursor.getColumnIndex("mark"))
                    );
        }
        return null;
    }

    /**
     * 删除收入信息
     * @param ids
     */
    public void delete(Integer... ids){
        if(ids.length>0){
            StringBuilder sb=new StringBuilder();
            for(int i=0;i<ids.length;i++){
                sb.append('?').append(',');
            }
            sb.deleteCharAt(sb.length()-1);
            db=helper.getWritableDatabase();
            db.execSQL("delete from tb_inaccount where _id in ("+sb+")",(Object[])ids);
        }
    }

    /**
     * 获取收入的信息
     * @param start 初始的位置
     * @param count  每页显示的数量
     * @return
     */
    public List<Tb_inaccount> getScrollData(int start,int count){
        List<Tb_inaccount> tb_inaccountList=new ArrayList<Tb_inaccount>();//初始化集合对象
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select * from tb_inaccount limit ?,?",new String[]{
                String.valueOf(start),String.valueOf(count)
        });
        while (cursor.moveToNext()){
            tb_inaccountList.add(new Tb_inaccount(
                    cursor.getInt(cursor.getColumnIndex("_id")),
                    cursor.getDouble(cursor.getColumnIndex("money")),
                    cursor.getString(cursor.getColumnIndex("time")),
                    cursor.getString(cursor.getColumnIndex("type")),
                    cursor.getString(cursor.getColumnIndex("handler")),
                    cursor.getString(cursor.getColumnIndex("mark"))
            ));
        }
        return tb_inaccountList;
    }

    /**
     * 获取总的记录数
     * @return
     */
    public long getCount(){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select count(_id) from tb_inaccount ",null);
        if(cursor.moveToNext()){
            return cursor.getLong(0);
        }
        return 0;
    }
    /**
     * 返回收入的最大编号
     * @return
     */
    public int getMaxId(){
        db=helper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select max(_id) from tb_inaccount ",null);
        if(cursor.moveToLast()){
            return cursor.getInt(0);
        }
        return 0;//没有数据,返回0
    }



}
最近下载更多
heweimin  LV12 4月15日
代码wuyu  LV2 2023年11月25日
dingruiasdad  LV3 2023年4月10日
懒耗子  LV3 2023年2月25日
222RRR  LV1 2022年12月12日
787194770  LV10 2022年10月19日
zhuimengren  LV4 2022年5月30日
GZW012345  LV8 2022年5月29日
1623096142  LV6 2022年5月27日
17岁的孩子想糖吃  LV7 2022年2月25日
最近浏览更多
heweimin  LV12 4月15日
wxb666  LV3 3月14日
代码wuyu  LV2 2023年11月25日
dzlwindy  LV8 2023年8月28日
Yanguu 2023年7月11日
暂无贡献等级
wersdfs  LV1 2023年7月8日
小妹妹  LV7 2023年6月9日
dingruiasdad  LV3 2023年4月10日
qazwsxedc0820 2023年3月20日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友