android笔记之SQLite 数据库操作

数据库的初始化

public class BlackListDb extends SQLiteOpenHelper {

    public BlackListDb(Context context) {
        super(context,"balcklist.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table blacklist_tb(_id integer primary key autoincrement" +
                ",phone text,mode integer)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        sqLiteDatabase.execSQL("drop table blacklist_tb");
        onCreate(sqLiteDatabase);

    }
}

数据库表的封装

public interface BlacklistTable {
    String PHONE="phone";
    String MODE="mode";
    String BLACKLIST_TB="blacklist_tb";
    int SMS=1<<0;//1为短信黑名单
    int TEL=1<<1;//2为电话黑名单
    int ALL=SMS|TEL;//短信和电话黑名单

}
public class BlacklistBean {
    String phone;

    public void setMode(int mode) {
        this.mode = mode;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    int mode;

    public String getPhone() {
        return phone;
    }

    public int getMode() {
        return mode;
    }
}


 对数据库的增删查改 
  


public class BlackListDao {
    private BlackListDb mBlackListDb;

    public  BlackListDao(Context context){
        this.mBlackListDb=new BlackListDb(context);
    }

    /**
     *向数据库中添加数据
     * @param phone
     * @param mode
     */
    public void add(String phone,int mode){
        SQLiteDatabase database=mBlackListDb.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(BlacklistTable.PHONE,phone);
        values.put(BlacklistTable.MODE,mode);
        database.insert("blacklist_tb",null,values);
        //将数据库关闭
        database.close();
    }
    //根据电话号码修改模式信息
    public void update(String phone,int mode){
        SQLiteDatabase database=mBlackListDb.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(BlacklistTable.MODE,mode);
        database.update(BlacklistTable.BLACKLIST_TB,values,BlacklistTable.PHONE+"=?",new String[]{phone});
        database.close();
    }
    //根据号码删除黑名单
    public void  delete(String phone){
        SQLiteDatabase database=mBlackListDb.getWritableDatabase();
        database.delete(BlacklistTable.BLACKLIST_TB,BlacklistTable.PHONE+"=?",new String[]{phone});
        database.close();
    }
    //获得所有的数据
    public List getAllDatas(){
        List datas=new ArrayList();
        SQLiteDatabase database = mBlackListDb.getReadableDatabase();
        //查询数据库
        Cursor cursor = database.rawQuery("select " + BlacklistTable.PHONE + "," + BlacklistTable.MODE + " from " + BlacklistTable.BLACKLIST_TB, null);
        //将数据封装发到bean中
        while (cursor.moveToNext()){
           BlacklistBean bean = new BlacklistBean();
            bean.setMode(cursor.getInt(1));
            bean.setPhone(cursor.getString(0));
            datas.add(bean);
        }
        cursor.close();//关闭游标
        database.close();//关闭数据库
        return datas;

    }
}


你可能感兴趣的:(android)