ios sqlite3_open 第 50 次调用失败

ghi*_*boz 2 sqlite database-connection objective-c ios

我有一个奇怪的问题,我希望解决:

这是我的代码:

-(Shot*) getShot:(int)shot {
    NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM tbShots where nShot = %d ", shot];
    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentFolderPath = [searchPaths objectAtIndex:0];
    NSString *dbFilePath = [documentFolderPath stringByAppendingPathComponent:DATABASE_NAME_EXT];

    if (dbFilePath == NULL) {
        NSLog(@"dbFilePath is NULL");
    }

    sqlite3 *dbHandle;
    if (sqlite3_open([dbFilePath UTF8String], &dbHandle)) {
        NSLog(@"sqlite3_open: failed");
    }

    sqlite3_stmt *preparedStatement;
    const char* queryStatement = [sqlStr UTF8String];
    sqlite3_prepare_v2(dbHandle, queryStatement, -1, &preparedStatement, NULL);

    Shot *s = nil;
    NSString * note = @"";
    while( sqlite3_step(preparedStatement) == SQLITE_ROW)
    {
        s = [[[Shot alloc] initWithShot:shot] autorelease];
    }

    sqlite3_finalize(preparedStatement);
    sqlite3_close(dbHandle);    
    return s;
}
Run Code Online (Sandbox Code Playgroud)

现在似乎工作正常:但我有一个问题:

我多次调用此函数,当我第 50 次调用它时,sqlite3_open 函数失败(我看到日志错误“sqlite3_open: failed”...

我犯了一些错误?提前致谢

dou*_*bou 5

我遇到了同样的问题 - 要解决它,您必须在应用程序启动时打开数据库,并在用户离开应用程序时关闭数据库。你不应该为每个查询打开和关闭数据库!!!!