2011-05-29 37 views
2

我正在使用SQLite數據庫編寫應用程序來存儲信息。我的數據庫表包含5個字段:gameID(主鍵,int),gameName(Varchar),isLocked,isHidden和isFavorite(所有bools)。我已經使用FireFox中的SQLite Manager設置了數據庫。從SQLite DB獲取BOOL時出現問題

+ (void) getInitialDataToDisplay:(NSString *)dbPath { 

FightingGamesGuideAppDelegate *appDelegate = (FightingGamesGuideAppDelegate *)[[UIApplication sharedApplication] delegate]; 

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) { 


     //My second thought is that this line is incorrect. (see below) 
    const char *sql = "select gameID, gameName, isLocked, isHidden, isFavorite from GameTable"; 
    sqlite3_stmt *selectstmt; 
    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) { 

     while(sqlite3_step(selectstmt) == SQLITE_ROW) { 


      NSInteger primaryKey = sqlite3_column_int(selectstmt, 0); 
      Games *coffeeObj = [[Games alloc] initWithPrimaryKey:primaryKey]; 
      //getGameName 
      coffeeObj.gameName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]; 

      //I think these three lines are where the problem is 
      coffeeObj.isLocked=(BOOL)sqlite3_column_int(selectstmt, 2); 
      coffeeObj.isHidden=(BOOL)sqlite3_column_int(selectstmt, 3); 
      coffeeObj.isFavorite=(BOOL)sqlite3_column_int(selectstmt, 4); 

       //This line is only to check if the data loaded properly 
      NSLog(@"%i, %i, %i",isLocked, isHidden, isFavorite); 


      [appDelegate.coffeeArray addObject:coffeeObj]; 

      [coffeeObj release]; 
     } 
    } 
} 
else{ 
    sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory. 
} 

} 

我的問題:這是如何獲得從SQLite數據庫的布爾?有沒有更好的方法,比如使用int作爲布爾代替?如果我的代碼是完全正確的,那麼您對於如何做到這一點還有其他建議嗎?

我知道除了這三行(和下面的行)之外的代碼能正常工作。在數據庫中,我已經將三個布爾值隨機分配給YES或NO。我也試過1和0,沒有任何效果。

如果行是正確的,我的第二個想法是,第9行是錯誤的。我修改它從「const ... =」選擇gameID,從GameTable的gameName「,並且gameID和gameName仍然正常工作。除了詢問是否有一個特定的「保存」按鈕/列表項,如果我更新了遊戲名稱,它顯示在我的應用程序,建議db自動保存。但是,如果我在正確的軌道上,我只需要這三條線路工作,並且我已完成(使用此部分)。

Aside-I know that除了印刷它們之外,bool變量沒有做任何事情控制檯。這是我的下一個挑戰。

謝謝你的時間!

回答

2

除了p.campbell的答案(+1),你可能還記得你的BOOL值這樣的,如果你是存儲YES/NO在DB:

coffeeObj.isLocked=[[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)] isEqualToString:@"YES"]; 
    coffeeObj.isHidden=[[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)] isEqualToString:@"YES"]; 
    coffeeObj.isFavorite=[[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)] isEqualToString:@"YES"]; 
+0

這似乎工作,謝謝。你能闡明爲什麼我的方法不起作用嗎?謝謝! – Jesse 2011-05-29 00:44:53

+0

那麼我還沒有看到你的代碼是/否版本。在你當前顯示的代碼中,我看到你試圖要求一個int並將其作爲'BOOL'投射出來。我從來沒有試過這個,也不確定它是否會以你所嘗試的方式行事。我需要測試一下。如果它是我使用int,我仍然會做一個比較,我肯定會產生一個'BOOL'。 'coffeeObj.isLocked =(sqlite3_column_int(selectstmt,2)== 1);'但那就是我。我會試試你的方式,看看我得到了什麼結果。 – dredful 2011-05-29 00:50:16

+0

我測試過你對'coffeeObj.isLocked =(BOOL)sqlite3_column_int(selectstmt,2);'的使用;假設你的數據庫確實存儲了一個'int',它看起來應該已經工作了。也許有可能你有其他東西調整錯了嗎? – dredful 2011-05-29 00:57:35

1

SQLite datatypes page有你要找的答案:

SQLite沒有一個單獨的布爾存儲類。相反,布爾值被存儲爲整數0(假)和1(真)。

+0

對不起,我應該提到, 。我確實找到了那個頁面。我的問題是爲了更像「爲什麼我不在做我正在做的事情」。通過從int轉換爲BOOL,我認爲我正在解決這個問題。 SQLite Manager爲Firefox添加了一個布爾選項,因此我的實驗是使用YES/NO和1/0。謝謝,不過。 – Jesse 2011-05-29 00:43:59