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 getAllData() {
        SQLiteDatabase db = helper.getReadableDatabase();
        @SuppressLint("Recycle") Cursor cursor = db.query("notes", null, null, null,
                null, null, "notesId desc");
        List 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();

你可能感兴趣的:(Sqlite的增删改查示例)