我有一个应用程序将数据存储在sqlite数据库中。我可以在sqlite中放入许多数据并更改我的数据。现在我想检查表sqlite中NSMutableArray
的值是多少?
我开始在sqlite中添加此数据,现在更改数据,我想检查并查看是否所有数组都在sqlite中没有更新,否则如果不更新它
这是我的代码:
NSArray *idd = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
NSArray *name = [NSArray arrayWithObjects:@"mamali",@"fred",@"jjjjjj",@"saeid",@"john", nil];
NSArray *sex = [NSArray arrayWithObjects:@"male",@"male",@"male",@"male",@"male", nil];
NSArray *is_deleted = [NSArray arrayWithObjects:@"1",@"1",@"1",@"1",@"1", nil];
for (int i = 0; i < [idd count]; i++)
{
NSString * ID = [idd objectAtIndex:i];
NSString * NAME = [name objectAtIndex:i];
NSString * SEX = [sex objectAtIndex:i];
NSString * DEL = [is_deleted objectAtIndex:i];
dic = [NSDictionary dictionaryWithObjectsAndKeys:ID,@"id",NAME,@"name",SEX,@"sex",DEL,@"deleted", nil];
if (!all) {
all = [NSMutableArray array];
}
[all addObject:dic];
}
告诉我如何检查所有是在sqlite还是没有???
答案 0 :(得分:0)
首先,您有内存管理问题。我建议你使用它来代替你的代码:
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代码取自here。