2012-08-02 74 views
0

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

这是我的代码:

-(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); 
} 

回答

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

Nvm。我在代码中放置了一些调试行,发现数据库路径为空,但与SQLITE_OK表达式相比,它仍然等于true。这有点愚蠢。问题是我必须手动将sqlite3文件放在项目文件夹中,然后通过xcode“添加到现有项目”。我最初在我的桌面上创建了数据库,并将项目放在一个单独的文件夹中。 “复制到文件夹”选项有效,但由于某些原因,XCode不会确认其路径。 :/修正它无论如何。感谢您的帮助。 – apples 2012-08-06 21:51:40

0

你不分配你的sqlStatement到你的compiledStatment。

const char *compiledStatment = [sqlStatement UTF8String];