1. 程式人生 > >資料庫封裝個單獨類裡面來完成我們的新增刪除的功能!

資料庫封裝個單獨類裡面來完成我們的新增刪除的功能!

package com.example.lianxi4.dao;

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

import com.example.lianxi4.bean.ContentBean;
import com.example.lianxi4.util.SqliteHepler;

import java.util.List;

public class UserDao {

private static UserDao instance;
private SqliteHepler hepler;
private SQLiteDatabase database;

public static UserDao getInstance(Context context){

    if(instance==null)
    {
        instance = new UserDao(context);
    }
    return instance;
}

public UserDao(Context context){
    hepler = new SqliteHepler(context);
    database = hepler.getReadableDatabase();
}


public void add(ContentBean.NewsList list){

    ContentValues values = new ContentValues();
    values.put("title",list.getTitle());
    values.put("date",list.getCtime());
    values.put("icon",list.getPicUrl());
    database.insert("user",null,values);

}

public void addAll(List<ContentBean.NewsList> lists){

    try{
        database.beginTransaction();
        for(ContentBean.NewsList bean:lists)
        {
            add(bean);
        }
        database.setTransactionSuccessful();
    }finally {
        database.endTransaction();
    }
}

public void delAll(){
    database.delete("user",null,null);
}

}