我正在處理離線模式的iPhone應用程序,其中第一次數據來自JSON格式的服務器,我解析這些數據並將其存儲到可變數組中,然後然後將這些數組值插入到sqlite數據庫中,但只插入一個值或最後一個值。只有最後一個值被插入到iPhone上的sqlite數據庫中
這裏是我的插入代碼:
-(void)insertData {
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath=[documentsDir stringByAppendingPathComponent:@"paddleEight.sqlite"];
NSLog(@"The Database Path is---> %@", databasePath);
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
int i;
sqlite3_stmt *addStmt;
NSString* sSqlSelect = [NSString stringWithFormat:@"insert into GalleryTabel(depth,imageUrl)VALUES(?,?);"];
for (i = 1; i < [self.propertiesArray count]; i++)
{
if(sqlite3_prepare_v2(database, [sSqlSelect UTF8String], -1, &addStmt, NULL) == SQLITE_OK)
{
sqlite3_bind_text(addStmt, 1, [[[[[self.propertiesArray objectAtIndex:i]objectForKey:@"images"]objectForKey:@"primary"]objectForKey:@"type"] UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [[[[[self.propertiesArray objectAtIndex:i]objectForKey:@"images"]objectForKey:@"primary"]objectForKey:@"location"] UTF8String],-1,SQLITE_TRANSIENT);
}
}
if(sqlite3_step(addStmt)==SQLITE_DONE)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Record" message:@"Contact Added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert=nil;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"record" message:@"record not created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert=nil;
}
}
}
任何幫助,高度讚賞。在高級感謝:
你檢查過你的數組大小嗎? – Hiren 2012-02-15 12:03:43
感謝您回覆我。是的,我已經檢查了有66個對象。 – iPhone4s 2012-02-15 12:15:37