表结构
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);