当我将数组元素插入 sqlite 时,它会正确插入。它只需要数组中的第一个元素,其余元素在插入时会显示错误。我的代码是:
NSString *path=[self getDBPath];
sqlite3_stmt *insert_statement = nil;
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
for (int i = 0; i < [array count]; i++)
{
NSLog(@"Array Count is %d",[array count]);
NSString *name=[[array objectAtIndex:i]objectForKey:@"name"];
NSString *add=[[array objectAtIndex:i]objectForKey:@"address"];
if (insert_statement == nil) {
NSString *statement1 = [NSString stringWithFormat:@"insert into tableName (name,address) Values( ?,?)",name,add];
const char *insertSql = [statement1 UTF8String];
if(sqlite3_prepare_v2(database, insertSql, -1, &insert_statement, NULL) != SQLITE_OK){
NSLog(@"Error while creating insert statement.");
insert_statement = nil;
continue;
}
sqlite3_bind_text(insert_statement, 1, [name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 2, [add UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(insert_statement)){
//NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
NSLog(@"Error while inserting data.");
insert_statement = nil;
continue;
}
else{}
sqlite3_reset(insert_statement);
sqlite3_finalize(insert_statement);
insert_statement = nil;
}
}
sqlite3_close(database);
}
为什么它只允许一条记录?