为什么sqlite3_prepare_v2 == FALSE?

时间:2012-08-02 17:45:33

标签: iphone sqlite xcode4.3

不确定为什么sqlite3_prepare_v2等于false。我在网上搜索过,找不到太多,我发现的东西没有帮助。一个站点建议使用sqlite3_errmsg(数据库),由于某种原因输出“不是错误”。在线的另一个答案建议删除iPhone模拟器中的一个文件夹,该文件夹以字符串十六进制命名,但这也不起作用。我创建了我的数据库并将其放入支持文件文件夹,因此它就在那里并且有记录。

这是我的代码:

-(void)readMovesFromDatabaseWithPath:(NSString *)filePath
{
    sqlite3 *database;

    printf("Here in readMovesFromDatabaseWithPath\n");

    if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
    {
        NSLog(@"Now in readMovesFromDatabaseWithPath\n");

        const char *sqlStatement = "select * from moves";
        sqlite3_stmt *compiledStatment;

        printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) ); //returns "not an error"

        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatment, NULL) == SQLITE_OK)
        {
            NSLog(@"In sqlite3_prepare_v2 block\n"); //does not reach this line

            while(sqlite3_step(compiledStatment) == SQLITE_ROW) //Loops through the database
            {
                //Extracts the move's name
                NSString *moveName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatment, 1)];

                //Extracts the move's description
                NSString *moveDescription = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatment, 2)];

                //Creates new move objects
                Moves *newMove = [[Moves alloc] init];
                newMove.moveName = moveName;
                newMove.moveDescription = moveDescription;
                [self.moves addObject:newMove];
            }
        }

        sqlite3_finalize(compiledStatment);
    }
    sqlite3_close(database);
}

2 个答案:

答案 0 :(得分:0)

尝试将打印错误的语句移动到else子句中。你的程序会告诉你它失败的原因:

    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatment, NULL) == SQLITE_OK)
    {
       /* your code here */
    }
    else
    {
        printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );
    } 

最有可能的情况是表moves不存在。

答案 1 :(得分:0)

您没有将sqlStatement分配给您的compiledStatment。

const char *compiledStatment = [sqlStatement UTF8String];
相关问题