数据库

public class Dao  {


    private final Myopenhelp m;
    private final SQLiteDatabase db;
    private String jsonstr;

    public Dao(Context ctx) {
        m = new Myopenhelp(ctx);

        db = m.getWritableDatabase();
    }

    //插入以及删除
    public void tianjia(String url,String jsonstr){

        db.delete("Use","url=?",new String []{jsonstr});
        //然后进行添加
        ContentValues values=new ContentValues();
        values.put("url",url);
        values.put("jsonstr",jsonstr);
        db.insert("Use",null, values);
        db.close();
    }

    //进行查询,根据url获取json串
    public String chaxun(String url){
        Cursor use = db.query("Use", null, "url=?", new String[]{url}, null, null, null);
while (use.moveToNext())
{

    jsonstr = use.getString(use.getColumnIndex("jsonstr"));
}

        return jsonstr;
    }




public class Myopenhelp extends SQLiteOpenHelper {
    public Myopenhelp(Context context) {
        super(context, "diyizhou.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建表
        db.execSQL("create table Use(id integer primary key,url text not null,jsonstr text not null)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {



    }

    Dao dao=new Dao(MainActivity.this);
            String chaxun = dao.chaxun(url);
            //当不为空的时候直接解析
if (chaxun!=null)
{
    Gson g=new Gson();
    Jie jie = g.fromJson(chaxun, Jie.class);
    Jie.DataBean.WeatherBean weather = jie.getData().getWeather();
    String city = jie.getData().getCity();

    tv.setText(city+weather.getTomorrow_condition()+weather.getDat_low_temperature()+"---"+weather.getDat_high_temperature()+weather.getUpdate_time()+"                        明天                "+weather.getTomorrow_weather_icon_id()+weather.getTomorrow_condition()+weather.getTomorrow_low_temperature()+"天气"+weather.getTomorrow_high_temperature()+"空气指数"+weather.getTomorrow_quality_level());


}else{
    
    //否则请求数据
    qingqiu();

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值