package zhanghaijiao.bawei.com.zhanghaijiao180330.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
/**
* Created by jane on 2018/4/1.
*/
public class JsonCacheDao {
private MyHelper myHelper;
public JsonCacheDao(Context context) {
myHelper = new MyHelper(context);
}
//添加方法
public void insertData(String path,String json){
//得到一个可读可写的数据库
SQLiteDatabase database=myHelper.getWritableDatabase();
//先删除,再插入
database.delete("jsoncache"," urlpath=?",new String[]{path});
ContentValues contentValues=new ContentValues();
//key:一定要是数据表中的列名
contentValues.put("urlpath",path);
contentValues.put("jsondata",json);
//第二个参数:强制要插入null值的列的列名
long rowid = database.insert("jsoncache", null, contentValues);
Log.d("zzz","插入的数据rowid:"+rowid);
}
//查询方法,根据url来查询数据
public String queryData(String path){
Log.d("zzz","---正在查询数据---");
String data="";
SQLiteDatabase db = myHelper.getWritableDatabase();
//查询
Cursor cursor = db.query("jsoncache", null, " urlpath=?", new String[]{path}, null, null, null);
while (cursor.moveToNext()){
data=cursor.getString(cursor.getColumnIndex("jsondata"));
}
return data;
}
}
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
/**
* Created by jane on 2018/4/1.
*/
public class JsonCacheDao {
private MyHelper myHelper;
public JsonCacheDao(Context context) {
myHelper = new MyHelper(context);
}
//添加方法
public void insertData(String path,String json){
//得到一个可读可写的数据库
SQLiteDatabase database=myHelper.getWritableDatabase();
//先删除,再插入
database.delete("jsoncache"," urlpath=?",new String[]{path});
ContentValues contentValues=new ContentValues();
//key:一定要是数据表中的列名
contentValues.put("urlpath",path);
contentValues.put("jsondata",json);
//第二个参数:强制要插入null值的列的列名
long rowid = database.insert("jsoncache", null, contentValues);
Log.d("zzz","插入的数据rowid:"+rowid);
}
//查询方法,根据url来查询数据
public String queryData(String path){
Log.d("zzz","---正在查询数据---");
String data="";
SQLiteDatabase db = myHelper.getWritableDatabase();
//查询
Cursor cursor = db.query("jsoncache", null, " urlpath=?", new String[]{path}, null, null, null);
while (cursor.moveToNext()){
data=cursor.getString(cursor.getColumnIndex("jsondata"));
}
return data;
}
}