當APP進入后臺或其他情況時,需刪除本地數(shù)據(jù)庫中的數(shù)據(jù),此時想簡單的得到符合查詢條件的記錄條數(shù),比如數(shù)據(jù)庫中的元素個數(shù)大于某個值時萍恕,就刪除前面的數(shù)據(jù)(也就是最早的數(shù)據(jù)),達到數(shù)據(jù)庫中最大緩存元素的個數(shù)车要,使用FMDB允粤,2條sqlite語句搞定
/// 清除本地數(shù)據(jù)庫中的數(shù)據(jù)
- (void)clearLivesData {
[self.dbQueue inDatabase:^(FMDatabase *db) {
[db open];
// 先查詢表中行數(shù)的總個數(shù)
NSUInteger count = [db intForQuery:@"select count(*) from t_lives"];
// NSLog(@"%ld",count);
// 當緩存的數(shù)據(jù)大于50條時就刪除前面的20數(shù)據(jù)
if (count > 50) {
NSString *delegateSql = @"DELETE FROM t_lives WHERE id < 20 ";
if ([db executeUpdate:delegateSql withArgumentsInArray:nil]) {
[self xy_showMessage:@"已刪除本地緩存中最早的20條數(shù)據(jù)"];
} else {
[self xy_showMessage:@"刪除本地數(shù)據(jù)失敗"];
}
}
[db close];
}];
}