2011-08-10 41 views
1

我想從數據庫中使用FMDB獲取數據,但嘗試過一段時間徒勞無功,我找不出爲什麼這會引發EXC_BAD_ACCESS異常。該參數是從NSInteger傳入的,但我既不能記錄也不能在SELECT語句中使用它。我的代碼是:NSInteger和int給出例外

-(SQLiteResult*) getClient:(bool)forceRefresh id:(NSString*)id forYear:(NSInteger)year { 
    SQLiteResult *res; 
    if(!forceRefresh){ 
     NSMutableArray *hereditaments = [[NSMutableArray alloc] init]; 
     NSLog([NSString stringWithFormat:@"SELECT * FROM Clients WHERE ID = %@ AND Year = %@", id, [NSString stringWithFormat:@"%@", year]]); // exception 
     NSLog([NSString stringWithFormat:@"SELECT * FROM Clients WHERE ID = %@ AND Year = %@", id, year]]); // also exception 
     FMResultSet *rs = [db executeQuery:[NSString stringWithFormat: @"SELECT * FROM Clients WHERE ID = ? AND Year = %@", year], id]; // also exception 
     FMResultSet *rs = [db executeQuery:[NSString stringWithFormat: @"SELECT * FROM Clients WHERE ID = ? AND Year = ?", id, year]]; // also exception 

我就只是把它當作一個字符串,而不是邊緣。有沒有一種安全的方法可以在SQL語句中使用這一年?

回答

0

正如你一年變量是NSInteger的類型,你應該使用%d格式,而不是%@在你的NSString創作

+0

將其格式化爲%d,而不是將其作爲參數傳遞給查詢。謝謝。 – Echilon

3

NSInteger不是一個對象類型。它只是一個typedef類型int。所以,你不能使用%@作爲格式說明符。你應該使用%d