DBhelper

public class DBhelper {//增删改查
    SQLiteOpenHelper dbHandler;
    SQLiteDatabase db;
    private static final String[] colums={
            NoteDatabase.ID,
            NoteDatabase.CONTENT,
            NoteDatabase.TIME,
            NoteDatabase.MODE
    };
    public DBhelper(Context context){
        dbHandler=new NoteDatabase(context);
    }
    public void open(){
        db=dbHandler.getWritableDatabase();
    }
    public void close(){
        dbHandler.close();
    }
    public Note addNote(Note note){//把Note加入到Database——插入
        ContentValues contentValues=new ContentValues();
        contentValues.put(NoteDatabase.CONTENT,note.getContent());
        contentValues.put(NoteDatabase.TIME,note.getTime());
        contentValues.put(NoteDatabase.MODE,note.getTag());
        long insertID=db.insert(NoteDatabase.TABLE_NAME,null,contentValues);
        note.setId(insertID);
        return note;
    }

    public Note getNote(long id){//获取一条笔记
        Cursor cursor=db.query(NoteDatabase.TABLE_NAME,colums,NoteDatabase.ID+"=?",
                new String[]{String.valueOf(id)},null,null,null,null);
        if (cursor!=null){
            cursor.moveToFirst();
        }
        Note e=new Note(cursor.getString(1),cursor.getString(2),cursor.getInt(3));
        return e;
    }
    public List<Note> getAllNotes(){
        Cursor cursor=db.query(NoteDatabase.TABLE_NAME,colums,null,null,null,null,null);
        List<Note> notes=new ArrayList<>();
        if (cursor.getCount()>0){
            while (cursor.moveToNext()){
                Note note=new Note();
                note.setId(cursor.getLong(cursor.getColumnIndex(NoteDatabase.ID)));
                note.setContent(cursor.getString(cursor.getColumnIndex(NoteDatabase.CONTENT)));
                note.setTime(cursor.getString(cursor.getColumnIndex(NoteDatabase.TIME)));
                note.setTag(cursor.getInt(cursor.getColumnIndex(NoteDatabase.MODE)));
                notes.add(note);
            }
        }
        return notes;
    }
    public int updateNote(Note note){
        ContentValues values=new ContentValues();
        values.put(NoteDatabase.CONTENT,note.getContent());
        values.put(NoteDatabase.TIME,note.getTime());
        values.put(NoteDatabase.MODE,note.getTag());
        return db.update(NoteDatabase.TABLE_NAME,values,NoteDatabase.ID+"=?",new String[]{String.valueOf(note.getId())});
    }
    public void removeNote(Note note){
        db.delete(NoteDatabase.TABLE_NAME,NoteDatabase.ID+"="+note.getId(),null);
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值