Ios sqlite多个打开一个失败

Ios sqlite多个打开一个失败,ios,sqlite,Ios,Sqlite,我在iPhone应用程序中有sqlite数据库,我在那里存储问题。 我得到一个问题,用户回答是或否,结果显示在第二个viewController上,当第二个viewController关闭时,我得到另一个问题 在30-40个问题之后,一切都很完美,突然之间,程序无法打开数据库。 如果(sqlite3_open([DBPath UTF8String],&database)==SQLITE_OK),则会失败 有答案吗 此功能: //从数据库中获取问题 - (void)getQuestion:(int

我在iPhone应用程序中有sqlite数据库,我在那里存储问题。 我得到一个问题,用户回答是或否,结果显示在第二个viewController上,当第二个viewController关闭时,我得到另一个问题

在30-40个问题之后,一切都很完美,突然之间,程序无法打开数据库。 如果(sqlite3_open([DBPath UTF8String],&database)==SQLITE_OK),则会失败

有答案吗

此功能:

//从数据库中获取问题

- (void)getQuestion:(int)getQ
{
    NSLog(@"getQuestion Started");

    sqlite3 *database;

    sqlite3_stmt *compiledStatement = NULL;

    if(sqlite3_open([DBPath UTF8String], &database) == SQLITE_OK)
    {
        NSLog(@"sqlite opened");

        const char *sql = "Select QuestionID, Question from cQuestions WHERE ExerciseLinID = ? ORDER BY QuestionID LIMIT ?, 1";

        if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating detail view statement. '%s'", sqlite3_errmsg(database));

        int curExID = [[listOfExID objectAtIndex:ExSel] integerValue];
        sqlite3_bind_int(compiledStatement, 1, curExID);
        sqlite3_bind_int(compiledStatement, 2, getQ);

        if(SQLITE_DONE != sqlite3_step(compiledStatement))
        {
            NSLog(@"Got one record");

            selectedQues = sqlite3_column_int(compiledStatement, 0);

            NSLog(@"selectedQues = %i", selectedQues);

            const char *QuNam = (char *)sqlite3_column_text(compiledStatement, 1);

            if(QuNam == nil)
                NSLog(@"!!! No data found.");
            else
            {
                if( iWhat == YES )
                    _labQuestion.text = [[NSString alloc] initWithCString:QuNam encoding:NSASCIIStringEncoding];
                else
                    _labQuestionPad.text = [[NSString alloc] initWithCString:QuNam encoding:NSASCIIStringEncoding];
            }
        }
    }
    else
    {
        NSLog(@"!!! Open error. %s", sqlite3_errmsg(database));
        NSLog(@"!!! Open error. %d", sqlite3_errcode(database));
    }
    sqlite3_close(database);
}

添加一些NSLog以了解哪个是错误

if(sqlite3_open([DBPath UTF8String], &database) == SQLITE_OK) {
    // Your Code
}
else {
    NSLog(@"sqlite3_open failed. Error:%d. %s", sqlite3_errcode(database), sqlite3_errmsg(database));
}
一个潜在的问题是,您没有关闭数据库,是吗

sqlite3_close(database);

使用
sqlite3\u errmsg
检查错误。这是一个错误:打开错误。无法打开数据库文件使用数据库后是否关闭数据库?sqlite3_errmsg报告了什么?失败的返回代码是什么?我的猜测是您没有关闭DB文件,并且您没有为进程打开可用的文件。(检查关闭时返回的代码。)我正在关闭数据库,它工作了40次,然后失败。发布的代码中没有什么特别的错误。很可能我们需要更多的代码来理解这个问题(你是否在回溯线程中做了一些与db相关的事情?),但是,解决这个问题的一种方法是重新设计你的代码,只使用1 open和1 close,如果多次打开\关闭db没有什么错的话。同样,我没有看到更新的代码有什么不好的地方,我还建议NSLog
[DBPath UTF8String]
,以确保在打开失败时,路径是预期的路径。