-2

我有一个将数据存储在 sqlite 数据库中的应用程序。我可以在 sqlite 中放入许多数据并且我的数据发生了变化。现在我想检查一下NSMutableArraysqlite表中的值???我开始在 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 中?

4

1 回答 1

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 代码取自这里

于 2013-05-02T10:25:00.983 回答