【发布时间】:2012-02-15 11:42:01
【问题描述】:
我正在开发一个离线模式的 iPhone 应用程序,其中第一次数据以 JSON 格式来自服务器,我解析这些数据,将其存储到可变数组中,然后将这些数组值插入到 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;
}
}
}
非常感谢任何帮助。感谢高级:
【问题讨论】:
-
你检查过你的数组大小了吗?
-
感谢您回复我。是的,我检查过有 66 个对象。
标签: iphone objective-c ios sqlite