首先,您有内存管理问题。我建议您使用以下代码,而不是您的代码:
NSArray *identifiersArray = @[ @"1",@"2",@"3",@"4",@"5" ];
NSArray *namesArray = @[ @"mamali",@"fred",@"jjjjjj",@"saeid",@"john" ];
NSArray *sexArray = @[ @"male",@"male",@"male",@"male",@"male" ];
NSArray *isDeletedArray = @[ @"1",@"1",@"1",@"1",@"1" ];
NSMutableArray *allArray = [NSMutableArray new];
[identifiersArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *identifier = [identifiersArray objectAtIndex:idx];
NSString *name = [namesArray objectAtIndex:idx];
NSString *sex = [sexArray objectAtIndex:idx];
NSString *isDeletedStr = [isDeletedArray objectAtIndex:idx];
NSDictionary *dic = @{ @"id" : identifier, @"name" : name, @"sex" : sex, @"deleted" : isDeletedStr };
[allArray addObject:dic];
}];
然后您可以使用类似的方法将数据插入到 sqlite 数据库:
// configuring SQL query
NSString *query = [NSString stringWithFormat:@"insert into ..."];
sqlite3_stmt *statement = nil;
const char *sql = [query UTF8String];
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK) {
// insert successfully executed
// all your data in database
}
然后您可以使用类似的方法从 sqlite 数据库中选择插入的数据:
// configuring select SQL query
NSString *query = [NSString stringWithFormat:@"select * from ..."]
sqlite3_stmt *statement = nil;
const char *sql = [query UTF8String];
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK) {
NSLog(@"[SQLITE] Error when preparing query!");
} else {
NSMutableArray *result = [NSMutableArray array];
while (sqlite3_step(statement) == SQLITE_ROW) {
NSMutableArray *row = [NSMutableArray array];
for (int i=0; i<sqlite3_column_count(statement); i++) {
int colType = sqlite3_column_type(statement, i);
id value;
if (colType == SQLITE_TEXT) {
const unsigned char *col = sqlite3_column_text(statement, i);
value = [NSString stringWithFormat:@"%s", col];
} else if (colType == SQLITE_INTEGER) {
int col = sqlite3_column_int(statement, i);
value = [NSNumber numberWithInt:col];
} else if (colType == SQLITE_FLOAT) {
double col = sqlite3_column_double(statement, i);
value = [NSNumber numberWithDouble:col];
} else if (colType == SQLITE_NULL) {
value = [NSNull null];
} else {
NSLog(@"[SQLITE] UNKNOWN DATATYPE");
}
[row addObject:value];
}
[result addObject:row];
}
return result;
}
return nil;
希望这会帮助你。sqlite 代码取自这里。