iphone - sqlite3_prepare 错误 #26

标签 iphone objective-c ios xcode sqlite

我有以下代码(来自 tutorial 的修改代码):

    -(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管理器中修改它

最佳答案

确保将数据库添加到项目的资源包 中,并且不要在数据库名称中放置任何空格。例如,使用像 champion db.sqlite 这样的名称。

关于iphone - sqlite3_prepare 错误 #26,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12556670/

相关文章:

objective-c - 覆盖 objective-c 中的可变参数方法

ios - -[__NSCFDictionary objectAtIndexedSubscript :]: unrecognized selector sent to instance

ios - 条件绑定(bind)的初始化程序必须具有可选类型,而不是 'Substring.SubSequence'(又名 'Substring')-swift

ios - 如何在以前为 iPhone 构建的应用程序中为 iPad 添加 .xib 文件?

iphone - project.pbxproj 中的 subversion 冲突正常吗?

iPhone 应用程序从文件内容中抛出 EXC_BAD_ACCESS 和字典

objective-c - (OS X) 确定文件是否正在写入?

ios - performSelector 或直接

iphone - UITableViewCellSeparatorStyleNone 不适用于 UITableViewStyleGrouped

iphone -- 在第二代 iPod Touch 上运行 XCode 4.2 和 iOS 5 SDK