0

我有以下代码(本教程中的修改代码):

    -(NSMutableArray *) getChampionDatabase
{
    NSMutableArray *championsArray = [[NSMutableArray alloc] init];

    @try {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *databasePath = [[NSBundle mainBundle]pathForResource: @"Champions Database" ofType:@"sqlite"];
    BOOL success = [fileManager fileExistsAtPath:databasePath];

    if (!success)
    {
        NSLog(@"cannot connect to Database! at filepath %@",databasePath);
    }

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
    const char *sql = "SELECT CHAMPION_ID,CHAMPION_NAME,CHAMPION_IMG FROM CHAMPIONS";
    sqlite3_stmt *sqlStatement;

    if(sqlite3_prepare(database, sql, -1, &sqlStatement, NULL) == SQLITE_OK)
    {
        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
            championList *ChampionList = [[championList alloc]init];
            ChampionList.championId = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,0)];
            ChampionList.championName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
            ChampionList.championImage = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 2)];
            [championsArray addObject:ChampionList];
    }
    }
        else
        {
            NSLog(@"problem with database prepare");
        }
    }
        else
        {
            NSLog(@"problem with database openning");
        }
    }

@catch (NSException *exception)
    {
    NSLog(@"An exception occured: %@", [exception reason]);
    }
@finally
    {
    return championsArray;
    }
}

运行此代码后,我总是有输出:“数据库准备问题”

然后我尝试通过以下代码检查 sqlite3_prepare 的错误号:

int ret = sqlite3_prepare(database, sql, -1, &sqlStatement, NULL);
            if (ret != SQLITE_OK) {
                NSLog(@"Error calling sqlite3_prepare: %d", ret);
            }

输出:“调用 sqlite3_prepare 时出错:26”

错误 26 - /*打开的文件不是数据库文件 */


文件有 sqlite 3 版本

这怎么可能?文件扩展名是 .sqlite,我可以在 sqlite manager 中修改它

4

1 回答 1

2

确保您将数据库添加到ressource bundle项目中,并且不要在您的数据库名称中添加任何空格。例如,使用类似的名称champion_db.sqlite

于 2012-09-23T22:19:36.013 回答