FMDatabase数据库已锁定

时间:2015-10-21 20:05:21

标签: objective-c fmdb

所以,我创建了一个存储一些数据的应用程序。但是,每次在我调用我编写的删除函数,然后调用其他函数后,我都会收到错误database locked

这是功能:

+ (void)deleteRestaurants:(NSString *)number {
    FMDatabase *database = [sDatabase openUp];

    [database open];

    FMResultSet *rs = [database executeQuery:[NSString stringWithFormat:@"SELECT * FROM restaurants WHERE id='%@'", number]];
    if ([rs next]) {
        NSString *cloudKit = [rs stringForColumn:@"CloudKit"];

        NSLog(@"CloudKit is %@", cloudKit);

        CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:cloudKit];

        NSLog(@"recordID is %@", recordID);

        [[CKContainer defaultContainer].privateCloudDatabase deleteRecordWithID:recordID completionHandler:^(CKRecordID *recordID, NSError *error) {
            NSLog(@"%@", error);
        }];
    }

    NSString *query = [NSString stringWithFormat:@"delete from restaurants where id='%@'", number];
    [database executeUpdate:query];
}

我做错了什么?或者我该如何解决/阻止这种情况?

2 个答案:

答案 0 :(得分:1)

user4992124我将为您提供删除数据的示例代码。请自定义有关编码的代码。

- (IBAction)actionDelete:(id)sender
{
   NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *docDir = [dirPaths objectAtIndex:0];
   NSString *databasePath = [docDir stringByAppendingPathComponent:@"yourDBName.sqlite"];

  FMDatabase *database = [FMDatabase databaseWithPath:databasePath];

  [database open];

  [database executeUpdate:@"delete from phone where id = ?",number];

  UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Sample SQLite" message:@"Deleted Successfully" delegate:Nil cancelButtonTitle:@"OK"otherButtonTitles: nil];
  [alert show];

  [database close];

} 

答案 1 :(得分:0)

抢劫市长是对的。你需要[rs close]。看起来像这样:

FMResultSet *rs = [database executeQuery:[NSString stringWithFormat:@"SELECT * FROM restaurants WHERE id='%@'", number]];
if ([rs next]) {
    NSString *cloudKit = [rs stringForColumn:@"CloudKit"];

    NSLog(@"CloudKit is %@", cloudKit);

    CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:cloudKit];

    NSLog(@"recordID is %@", recordID);

    [[CKContainer defaultContainer].privateCloudDatabase deleteRecordWithID:recordID completionHandler:^(CKRecordID *recordID, NSError *error) {
        NSLog(@"%@", error);
    }];
}
[rs close];