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;
}
}