0

不知道为什么 sqlite3_prepare_v2 等于 false。我在网上搜索并找不到太多,我找到的也没有帮助。一个站点建议使用 sqlite3_errmsg(database) 并且由于某种原因输出“不是错误”。另一个在线答案建议删除 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);
}
4

2 回答 2

0

您没有将您的 sqlStatement 分配给您的已编译状态。

const char *compiledStatment = [sqlStatement UTF8String];
于 2012-12-25T08:56:24.577 回答
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不存在。

于 2012-08-03T02:45:30.297 回答