2

我的数据库表中有 5 条记录,我需要显示第四条记录NSLog,但它不会进入IF代码语句。有人可以建议我错在哪里吗?

这是我的代码。

-(void) readFromDatabase {

    [self checkAndCreateDatabase];

    // Setup the database object
    sqlite3 *database;


    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        // Setup the SQL Statement and compile it for faster access
        const char *sqlStatement = "select * from QuestionAnswers where Q.No = 4";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                // Read the data from the result row

                NSString *questions = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                NSString *answers = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];

                NSLog(@"Questions:%@",questions);
                NSLog(@"Answers:%@",answers);


            }
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);

}

提前致谢。

4

1 回答 1

0

document你是从文件夹中获取数据库吗?如果是,则在执行代码之前检查是否database已正确复制到 Document 文件夹中。

- (BOOL) getFileExistence: (NSString *) filename
{
    BOOL IsFileExists = NO;

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    NSString *favsFilePath = [documentsDir stringByAppendingPathComponent:filename];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem
    if ([fileManager fileExistsAtPath:favsFilePath])
    {
        IsFileExists = YES;
    }
    return IsFileExists;
}

BOOL isDBExists = [self getFileExistence:@"dbname"];
于 2013-09-05T06:25:16.587 回答