SQLite数据库简单的增删改查

MyHelper:

	public class MyHelper extends SQLiteOpenHelper {
	    public MyHelper(Context context) {
	        super(context, "week3", null, 1);
	    }
	
	    @Override
	    public void onCreate(SQLiteDatabase db) {
	        String sql = "create table news(id integer,title text,content text)";
	        db.execSQL(sql);
	    }
	
	    @Override
	    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	
	    }
    }

Dao层:

public class Dao {

	    private SQLiteDatabase database;
	
	    public Dao(Context context) {
	        MyHelper helper = new MyHelper(context);
	        database = helper.getWritableDatabase();
	    }
	
	    public List cha(){
	        List list = new ArrayList<>();
	        Cursor cursor = database.query("news", null, null, null, null, null, null);
	        while (cursor.moveToNext()){
	            NewsBean.DataBean newsBean = new NewsBean.DataBean();
	            String news_id = cursor.getString(cursor.getColumnIndex("news_id"));
	            String news_summary = cursor.getString(cursor.getColumnIndex("news_summary"));
	            String news_title = cursor.getString(cursor.getColumnIndex("news_title"));
	            newsBean.setNews_id(news_id);
	            newsBean.setNews_summary(news_summary);
	            newsBean.setNews_title(news_title);
	            list.add(newsBean);
	        }
	        return list;
	    }
	
	    //添加
	    public void add(List list){
	        for (int i=0;i

解析完成之后调用Dao层的add添加方法,添加到数据库,然后调用cha方法,赋值即可

你可能感兴趣的:(SQLite数据库简单的增删改查)