iOS中的SQLite更新错误&x27;无法提交-没有活动的事务';

iOS中的SQLite更新错误&x27;无法提交-没有活动的事务';,ios,database,sqlite,Ios,Database,Sqlite,我正在尝试更新SQLite数据库,但遇到了问题。每次单击按钮进行更新时,都会出现以下错误: 无法提交-没有活动的事务 我看不出我的代码有什么问题,所以我不知道如何解决这个问题 -(BOOL)setup{ databasePath = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"sqlite"]; BOOL isSuccess = YES; NSFileManager *filemgr = [NSFileManager defa

我正在尝试更新SQLite数据库,但遇到了问题。每次单击按钮进行更新时,都会出现以下错误:

无法提交-没有活动的事务

我看不出我的代码有什么问题,所以我不知道如何解决这个问题

-(BOOL)setup{
databasePath = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"sqlite"];
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if (![filemgr fileExistsAtPath: databasePath ])
{
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        return  isSuccess;
        sqlite3_close(database);
    }
    else {
        isSuccess = NO;
        NSLog(@"Failed to open/create database");
    }
}

return isSuccess;
}


 - (NSMutableArray*) findWord:(int)ID
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
    NSString *querySQL = [NSString stringWithFormat:
                          @"select Word, Category, Clue, Length, Solved from X where ID=\"%i\"",ID];
    const char *query_stmt = [querySQL UTF8String];
    NSMutableArray *resultArray = [[NSMutableArray alloc]init];
    if (sqlite3_prepare_v2(database,
                           query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        if (sqlite3_step(statement) == SQLITE_ROW)
        {

            NSString *solved = [[NSString alloc]initWithUTF8String:
                                (const char *) sqlite3_column_text(statement, 4)];

            if ([solved intValue] == 0) {

                [resultArray addObject:[NSString stringWithFormat:@"%i", ID]];

                NSString *word = [[NSString alloc] initWithUTF8String:
                                  (const char *) sqlite3_column_text(statement, 0)];
                [resultArray addObject:word];

                NSString *cat = [[NSString alloc] initWithUTF8String:
                                   (const char *) sqlite3_column_text(statement, 1)];
                [resultArray addObject:cat];

                NSString *clue = [[NSString alloc]initWithUTF8String:
                               (const char *) sqlite3_column_text(statement, 2)];
                [resultArray addObject:clue];

                NSString *length = [[NSString alloc]initWithUTF8String:
                                  (const char *) sqlite3_column_text(statement, 3)];
                [resultArray addObject:length];
            }
            else {

            }
            sqlite3_reset(statement);
            sqlite3_close(database);
            return resultArray;
        }
        else{
            return nil;
        }

    }
    sqlite3_reset(statement);
}
sqlite3_close(database);
return nil;
}

- (void) findData{
NSMutableArray *m = [[NSMutableArray alloc] init];
m = [self findWord:arc4random() %10];
if (m == nil) {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:
                          @"Data not found" message:nil delegate:nil cancelButtonTitle:
                          @"OK" otherButtonTitles:nil];
    [alert show];
}
else {
    NSLog(@"%@", [m objectAtIndex:1]);
}


}

-(IBAction)updateClick:(id)sender{

sqlite3_stmt *updateStmt;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &database) == SQLITE_OK)
{
    const char *sql = "update X Set Solved = 1 Where ID=2";
    if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL)==SQLITE_OK){

    }
}
char* errmsg;
sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);

if(SQLITE_DONE != sqlite3_step(updateStmt)){
    NSLog(@"Error while updating. %s", sqlite3_errmsg(database));
}

sqlite3_finalize(updateStmt);
sqlite3_close(database);

}

任何建议都将不胜感激。

您正在从bundle本身访问数据库。您不能更新任何捆绑资源,因为它们已由证书签名。如果要编辑数据库,请首先将其复制到Documents目录,然后给出Documents目录位置的路径,而不是bundle位置的路径。这会解决你的问题