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];