android之SQLite数据库的增删改查

源自传智播客的教程,参考下视频教程即可

public class PersonService {
    private DBOpenHelper dbOpenHelper;
    
    public PersonService(Context context) {
        this.dbOpenHelper = new DBOpenHelper(context);
    }

    public void payment(){
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        db.beginTransaction();//开启事务
        try{
            db.execSQL("update person set amount=amount-10 where personid=?", new Object[]{1});
            db.execSQL("update person set amount=amount+10 where personid=?", new Object[]{2});
            db.setTransactionSuccessful();//设置事务标志为成功,当结束事务时就会提交事务,否则会回滚事务
        }finally{
            db.endTransaction();
        }
    }
    
    public void save(Person person){
        //如果要对数据进行更改,就调用此方法得到用于操作数据库的实例,该方法以读和写方式打开数据库
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        db.execSQL("insert into person (name,amount) values(?,?)",
                new Object[]{person.getName(),person.getAmount()});
    }
    
    public void update(Person person){
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        db.execSQL("update person set name=? where personid=?",
                new Object[]{person.getName(),person.getId()});
    }
    
    public void delete(Integer id){
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        db.execSQL("delete from person where personid=?", new Object[]{id.toString()});
    }
    
    public Person find(Integer id){
        //如果只对数据进行读取,建议使用此方法
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from person where personid=?", new String[]{id.toString()});
        if(cursor.moveToFirst()){
            int personid = cursor.getInt(cursor.getColumnIndex("personid"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int amount = cursor.getInt(cursor.getColumnIndex("amount"));
            Person person = new Person(personid, name);
            person.setAmount(amount);
            return person;
        }
        return null;
    }
    
    public List<Person> getScrollData(Integer offset, Integer maxResult){
        List<Person> persons = new ArrayList<Person>();
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from person limit ?,?",
                new String[]{offset.toString(), maxResult.toString()});
        while(cursor.moveToNext()){
            int personid = cursor.getInt(cursor.getColumnIndex("personid"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int amount = cursor.getInt(cursor.getColumnIndex("amount"));
            Person person = new Person(personid, name);
            person.setAmount(amount);
            persons.add(person);
        }
        cursor.close();
        return persons;
    }
    
    public Cursor getCursorScrollData(Integer offset, Integer maxResult){
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        return db.rawQuery(" select personid as _id/*这里表示android要求数据库的表中要有_id字段,所以起别名*/, name, amount from person limit ?,?",
                new String[]{offset.toString(), maxResult.toString()});
    }
    
    public long getCount() {
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select count(*) from person", null);
        cursor.moveToFirst();
        return cursor.getLong(0);
    }
}

你可能感兴趣的:(android,sqlite数据库的增删改查)