2011-05-16 68 views
0

我有一個表「關鍵字」,有2列「pk」和「文本」。在firefox SQLite管理器中,我可以看到「text」列中有兩個名字「john」和「tom」。我的代碼中沒有錯誤或警告。當我在模擬器中運行它時,我可以在控制檯中看到我從不進入While循環。這意味着FMResultSet「rs」沒有從SQL查詢中得到任何結果。我得到「Opened successfully」消息意味着我的數據庫沒有任何問題地打開,並且數據庫路徑,數據庫名稱等都是正確的。我的查詢也是正確的,因爲它沒有在控制檯中顯示任何查詢錯誤。但我沒有收到「while循環開始」的消息。我的數據庫數組也是空的,所以我知道while循環不起作用。我正在使用FMDatabase。這裏是我的代碼SQlite-FMDatabase查詢while循環不工作

-(void) readWordsfromDatabase 
{ 
db=[FMDatabase databaseWithPath:globalDatabasePath]; 
globalDatabaseArray=[[NSMutableArray alloc] init]; 
[db setLogsErrors:TRUE ]; 
[db setTraceExecution:TRUE]; 
if (![db open]) 
{ 
    NSLog(@"Failed to open database"); 
    return; 
} 
else { 
    NSLog(@"Opened successfully"); 
} 
FMResultSet *rs= [db executeQuery:@"SELECT * FROM keywords"]; 
while([rs next]) 
{ 
    NSLog(@"while loop started"); 

    int aPK=[rs intForColumn:@"pk"]; 
    NSString *aText=[rs stringForColumn:@"text"]; 
    NSLog(@"aText is %@",aText); 

    singleKeyword *sk=[[singleKeyword alloc] initWithData:aPK :aText]; 
    [globalDatabaseArray addObject:sk]; 
    [sk release]; 

      NSLog(@"text in array%@",[self.globalDatabaseArray objectAtIndex:0]); 
    NSLog(@"text in array%@",[self.globalDatabaseArray objectAtIndex:1]); 
}//while closed 
//NSLog(@"text in array%@",[self.globalDatabaseArray objectAtIndex:0]); 
    // NSLog(@"text in array%@",[self.globalDatabaseArray objectAtIndex:1]); 
[db close]; 
} 

回答

0

FMDB是打開數據庫無論如果文件路徑存在與否(實際上是FMDB還設計,你可以叫[FMDatabase databaseWithPath:nil],而不會在一個錯誤或警告)。如果您想檢查是否打開了「正確的」數據庫,則必須使用NSFileManager並驗證路徑。

- (BOOL)fileExistsAtPath:(NSString *)path 

最常見的錯誤是你的sqlite-manager和你的FMDB指的是不同的數據庫。