如何通过 'DBAccess' iOS ORM 删除具有不同 ID 的多行
How do I delete multiple rows with different Ids by 'DBAccess' iOS ORM
我在 Xcode 7.2.1.
中使用 DBAccess 框架 v1.6.12
我想通过指定 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];
}
[回复#1]
感谢您的评论。
我试过你的建议。但是无法解决。
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];
}
感谢您的帮助!
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];
我在 Xcode 7.2.1.
中使用 DBAccess 框架 v1.6.12我想通过指定 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];
}
[回复#1]
感谢您的评论。
我试过你的建议。但是无法解决。
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];
}
感谢您的帮助!
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];