我正在使用DBAccess框架v1.6.12和Xcode 7.2.1。
我想通过指定ID来删除多行。
代码图片:
// MyDataModel is a subclass of DBObject
const int startCount = [[MyDataModel query] count];
NSLog(@"startCount:%@", @(startCount)); // startCount:21
NSString *Ids = [removeIds componentsJoinedByString:@","];
NSLog(@"removingIds:%@", Ids); // removingIds:48,44,49,45,50,46,51,47,43
DBResultSet *rs = [[[MyDataModel query] whereWithFormat:@"Id in (%@)" withParameters:@[Ids]] fetch];
NSLog(@"resultSetCount:%@", @(rs.count)); // resultSetCount:0
[rs removeAll];
const int afterCount = [[MyDataModel query] count];
NSLog(@"afterCount:%@", @(afterCount)); // afterCount:21
也许,DBAccess不支持"Id in (?,?,?)"
格式查询?
请告诉我上面存档的解决方案。
当我编写如下代码时,它按预期工作。
这是合法的措施吗?
for (NSNumber *Id in removeIds) {
[[[[MyDataModel query] whereWithFormat:@"Id = %@" withParameters:@[Id]] fetchLightweight] removeAll];
}
感谢您的评论。
我试过你的建议。但它无法解决。
NSLog(@"before-delete:%@", @([[MyDataModel query] count]));
// -> before-delete:23
if (removeIds.count) {
NSLog(@"removeIds:%@", [removeIds componentsJoinedByString:@","]);
// -> removeIds:119,120
[[[[MyDataModel query] whereWithFormat:@"Id in (%@)" withParameters:removeIds] fetchLightweight] removeAll];
}
NSLog(@"after-delete:%@", @([[MyDataModel query] count]));
// -> after-delete:22
[removeIds enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"Id[%@]=%@", obj, @([[[MyDataModel query] whereWithFormat:@"Id = %@" withParameters:@[obj]] count]));
// -> Id[119]=0 .. 0 means succeeded to delete
// -> Id[120]=1 .. 1 means failed to delete
}];
我想删除Id = 119和Id = 120,但它只删除了一行。
有效!
毕竟,我写了下面的代码。
if (removeIds.count) {
[[[[MyDataModel query] whereWithFormat:@"Id IN (%@)" withParameters:@[removeIds]] fetchLightweight] removeAll];
}
感谢您的帮助!
答案 0 :(得分:0)
DBAccess确实支持' IN(?)'查询的样式,问题是Id应该作为NSArray传入。
NSArray* Ids = @[@(12), @(14), @(18)];
DBResultSet *rs = [[[MyDataModel query] whereWithFormat:@"Id IN (%@)" withParameters:@[Ids]];
或者你可以在obj-c中使用:
DBResultSet *rs = [[[MyDataModel query] whereWithFormat:@"Id IN (%@)",Ids];