Sqlite数据库添加,删除前n条记录

1.查询前N条记录:

       public Cursor query(String sql,String[] selectionArgs){

SQLiteDatabase db=
dbHelper.getReadableDatabase();
return db.rawQuery(sql,selectionArgs);
}

String sql2="SELECT * FROM "+ DetectDao.DETECT_TABLE+" order by "+DetectDao.KEY_ID+"  limit 20";

cursor=DetectDao.getInstance(MyService.this).query(sql2, null);

DetectDao.KEY_ID是自增ID。


2.删除前N条记录:

     public void delete(String sql){
    SQLiteDatabase db=dbHelper.getWritableDatabase();
    db.execSQL(sql);
    }

String sql="DELETE FROM  "+ DetectDao.DETECT_TABLE+"  where  "+DetectDao.KEY_ID+ " in (SELECT "+DetectDao.KEY_ID+" FROM "+ DetectDao.DETECT_TABLE+" order by "+DetectDao.KEY_ID+"  limit "+count+")";
   DetectDao.getInstance(MyService.this).delete(sql);

DetectDao.KEY_ID是自增ID。

你可能感兴趣的:(Sqlite数据库添加,删除前n条记录)