2012-03-28 33 views
1

我嘗試將Json數據插入到sqlite數據庫。我從另一個類獲得數據,它們在字典(responce.item)中,但我無法插入數據庫。我得到了這樣的錯誤:無法識別的選擇器發送到實例。我怎麼解決這個問題?我的方法如下。感謝您的回覆。- [__ NSArrayM objectForKey:]:無法識別的選擇器發送到實例目標中的錯誤c

編輯代碼:

-(void)processDoneWithRequestName:(NSString*)tagName{ 

sqlite3_stmt *stmt=nil; 
sqlite3 *cruddb; 

const char *sql = "INSERT INTO LabUpdate (IsSuccess, ProducerId, Latitude, Longitude, Altitude, Slope, SampleDate, PackageNo, Status, Description) VALUES (?,?,?,?,?,?,?,?,?,?)";    

//NSArray *pathsArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
NSString *[email protected]"/Users/ds/Desktop/SqliteTest/SqliteTest"; 
NSString *cruddatabase=[doumentDirectoryPath stringByAppendingPathComponent:@"SqliteTestDb.sqlite"]; 

if ([tagName isEqualToString:Logins]) { 

    int keys = [[response.item objectForKey:@"Lipton"] count]; 
    NSLog(@"count %i",keys); 
    for (int i=0; i<keys; i++) 
    { 

     NSString *str1 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"IsSuccess"]; 
     NSString *str2 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"ProducerId"]; 
     NSString *str3 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Latitude"]; 
     NSString *str4 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Longitude"]; 
     NSString *str5 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Altitude"]; 
     NSString *str6 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Slope"]; 
     NSString *str7 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"SampleDate"]; 
     NSString *str8 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"PackageNo"]; 
     NSString *str9 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Status"]; 
     NSString *str10 =[[[response.item objectForKey:@"Lipton"] objectAtIndex:i]objectForKey:@"Description"]; 
     NSLog(@"str1 %@",str1); 
     NSLog(@"str2 %@",str2); 
     NSLog(@"str3 %@",str3); 
     NSLog(@"str4 %@",str4); 
     NSLog(@"str5 %@",str5); 
     NSLog(@"str6 %@",str6); 
     NSLog(@"str7 %@",str7); 
     NSLog(@"str8 %@",str8); 
     NSLog(@"str9 %@",str9); 
     NSLog(@"str10 %@",str10); 




     sqlite3_open([cruddatabase UTF8String], &cruddb); 
     sqlite3_prepare_v2(cruddb, sql, 1, &stmt, NULL); 
     sqlite3_bind_int(stmt, 1, [str1 integerValue]); 
     sqlite3_bind_int(stmt, 2, [str2 integerValue]); 
     sqlite3_bind_double(stmt, 3, [str3 floatValue]); 
     sqlite3_bind_double(stmt, 4, [str4 floatValue]); 
     sqlite3_bind_double(stmt, 5, [str5 floatValue]); 
     sqlite3_bind_double(stmt, 6, [str6 floatValue]); 
     sqlite3_bind_text(stmt, 7, [str7 UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_int(stmt, 8, [str8 integerValue]); 
     sqlite3_bind_int(stmt, 9, [str9 integerValue]); 
     sqlite3_bind_text(stmt, 10, [str10 UTF8String], -1, SQLITE_TRANSIENT); 


     sqlite3_step(stmt); 
     sqlite3_finalize(stmt); 
     sqlite3_close(cruddb); 



    } 



} 

}

+0

哪條線有錯誤? – 2012-03-28 12:30:17

+0

你正在得到哪個錯誤。發佈整個錯誤。 – Bharathi 2012-03-28 12:59:57

+0

你可以發佈你的JSON負載的例子嗎? – Alladinian 2012-03-28 13:47:25

回答

4

該錯誤意味着該對象是一個NSArray不是NSDictionary。您可以使用-objectAtIndex:訪問字典對象。

編輯:很難從您所提供的信息說,但嘗試改變這一行:

int keys = [[response.item objectForKey:@"Lipton"] count]; 

這樣:

int keys = [[[response.item objectForKey:@"Lipton"] objectAtIndex:0] count]; 

或:

int keys = [[[response.item objectAtIndex:0] objectForKey:@"Lipton"] count]; 

就像我說的,沒有更多的信息,我不能給出決定性的答案。

+0

好的我使用objectAtIndex但鍵數1,所以我只能輸入1次。我現在能做什麼? – 2012-03-28 13:52:54

+0

請參閱我的答案上的編輯。 – edc1591 2012-03-28 18:33:32

+0

好吧我已經解決了這個問題,感謝您的回覆,但現在它在我的RequestResponce類崩潰了,我調用了上面的代碼[delegate processDoneWithRequestName:headerTag];我怎麼解決這個問題? – 2012-03-29 07:08:00

相關問題