新的数据库存储

package com.bwie.zjj.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;

public class SqlitOpenHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "news.db";
    private static final int DB_VERSION = 1;

    public SqlitOpenHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table news(_id INTEGER primary key autoincrement,url TEXT,json TEXT) ");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
/Dao层
package com.bwie.zjj.dao;


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

public class Dao {

    private final SqlitOpenHelper helper;
    private final SQLiteDatabase db;

    public Dao(Context context){
        helper = new SqlitOpenHelper(context);
        db = helper.getWritableDatabase();
    }

    /**
     * 插入或更新

     */
    public void put(DaoBean daoBean){
        ContentValues values = new ContentValues();
        values.put("url",daoBean.url);
        values.put("json",daoBean.json);
        db.replace("news",null,values);
    }
    /**
     * 查询
     */
    public DaoBean select(String url){
        DaoBean daobean=null;
        Cursor cursor = db.rawQuery("select * from news where url=?", new String[]{url});
        if(cursor.moveToNext()){
            daobean=new DaoBean();
            daobean.url=cursor.getString(cursor.getColumnIndex("url"));
            daobean.json=cursor.getString(cursor.getColumnIndex("json"));
        }
        return daobean;
    }
}
/DaoBean层
package com.bwie.zjj.dao;

import org.w3c.dom.Text;

public class DaoBean {
    public int _id;
    public String url;
    public String json;

    public DaoBean() {

    }

    public DaoBean(String url, String json) {
        this.url = url;
        this.json = json;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值