Sqlite的增刪改查示例

一、NoteDBOpenHelper

// 建數據庫和表

public class NoteDBOpenHelper extends SQLiteOpenHelper {
    public NoteDBOpenHelper(@Nullable Context context) {
        super(context, "notepad.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //不用自增建,因爲在外面沒法獲取到,用時間戳作爲每條的id,這樣也不會重複
        db.execSQL("CREATE TABLE IF NOT EXISTS notes (notesId varchar(200) ,title varchar(20), content varchar(200), time varchar(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

二、操作類

public class NoteSQLService {
NoteDBOpenHelper helper;

    public NoteSQLService(Context context) {
        helper = new NoteDBOpenHelper(context);
    }

    public long addInfo(Notepad notes) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("title", notes.getTitle());
        values.put("content", notes.getContent());
        values.put("time", notes.getTime());
        values.put("notesId",notes.getId());
        long rowId = -1;
        if (find(notes.getId()) != null){
            rowId =  update(notes);
        } else {
            rowId = db.insert("notes", null, values);
        }

        db.close();
        return rowId;
    }

    public Notepad find(String id) {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("notes", null, "notesId=?", new String[]{id + ""}, null, null, null);
        if (cursor.getCount() != 0) {
            if (cursor.moveToFirst()) {
                @SuppressLint("Range") String title = cursor.getString(cursor.getColumnIndex("title"));
                @SuppressLint("Range") String content = cursor.getString(cursor.getColumnIndex("content"));
                @SuppressLint("Range") String time = cursor.getString(cursor.getColumnIndex("time"));
                return new Notepad(id,title, content, time);
            }
        }
        return null;
    }

    public int delete(String id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        int rowNumber = db.delete("notes", "notesId=?", new String[]{id + ""});
        db.close();
        return rowNumber;
    }

    public int update(Notepad notes) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("title", notes.getTitle());
        values.put("content", notes.getContent());
        values.put("time", notes.getTime());
        int rowNumber = db.update("notes", values, "notesId=?", new String[]{notes.getId() + ""});
        db.close();
        return rowNumber;
    }

    public List<Notepad> getAllData() {
        SQLiteDatabase db = helper.getReadableDatabase();
        @SuppressLint("Recycle") Cursor cursor = db.query("notes", null, null, null,
                null, null, "notesId desc");
        List<Notepad> notes = new ArrayList<>();
        if (cursor.getCount() != 0) {
            while (cursor.moveToNext()) {
                @SuppressLint("Range") String id = cursor.getString(cursor.getColumnIndex("notesId"));
                @SuppressLint("Range") String title = cursor.getString(cursor.getColumnIndex("title"));
                @SuppressLint("Range") String content = cursor.getString(cursor.getColumnIndex("content"));
                @SuppressLint("Range") String time = cursor.getString(cursor.getColumnIndex("time"));
                notes.add(new Notepad(id, title, content, time));
            }
            return notes;
        }
        return null;


}
}

三、具體使用

service = new NoteSQLService(this);
notepads = service.getAllData();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章