1. 程式人生 > >Sqlite資料庫新增,刪除前n條記錄

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。