数据缓存

表结构


db.execSQL("create table cache(title varchar(20),json text)");

数据表添加方法

public void addCache(String title,String json){
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("insert into cache values(?,?)",new Object[]{title,json});
        db.close();
    }

数据表更新方法

 public void updateCache(String json,String title){
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("update cache set json=? where title=?",new Object[]{json,title});
        db.close();
    }

数据表查询方法

public List<CacheBean> queryCache(String title){
        List<CacheBean> list=new ArrayList<>();
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from cache where title=?", new String[]{title});
        while (cursor.moveToNext()){
            String title1=cursor.getString(0);
            String context=cursor.getString(1);
            list.add(new CacheBean(title1,context));
        }
        return list;
    }

Fragment操作数据库方法

                List<CacheBean> list=dao.queryCache(title);
                if (s==null) {
                    if (list.size()!=0){
                        s=list.get(0).getJson();
                    }else{
                        Toast.makeText(getActivity(),"没联网,无数据",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }

                if (list.size()==0){
                    dao.addCache(title,s);
                }
                dao.updateCache(s,title);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值