我正在使用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變量沒有做任何事情控制檯。這是我的下一個挑戰。
謝謝你的時間!
這似乎工作,謝謝。你能闡明爲什麼我的方法不起作用嗎?謝謝! – Jesse 2011-05-29 00:44:53
那麼我還沒有看到你的代碼是/否版本。在你當前顯示的代碼中,我看到你試圖要求一個int並將其作爲'BOOL'投射出來。我從來沒有試過這個,也不確定它是否會以你所嘗試的方式行事。我需要測試一下。如果它是我使用int,我仍然會做一個比較,我肯定會產生一個'BOOL'。 'coffeeObj.isLocked =(sqlite3_column_int(selectstmt,2)== 1);'但那就是我。我會試試你的方式,看看我得到了什麼結果。 – dredful 2011-05-29 00:50:16
我測試過你對'coffeeObj.isLocked =(BOOL)sqlite3_column_int(selectstmt,2);'的使用;假設你的數據庫確實存儲了一個'int',它看起來應該已經工作了。也許有可能你有其他東西調整錯了嗎? – dredful 2011-05-29 00:57:35