Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I create one application that store data from NSDictionary in SQLite. this NSDictionary get own information from many NSArray and store in SQLite DB. this is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    int number = [self GetNumberOfRecord]; //this method get number of rows in sqlite
    NSArray *idd = @[@"122", @"234", @"453", @"53464", @"4565", @"1111", @"2222"];
    NSArray *name = @[@"janatan", @"fred", @"john", @"cristiano", @"vein", @"emma", @"shyla"];
    NSArray *age = @[@"23", @"35", @"12", @"24", @"22", @"34", @"56"];
    NSArray *sex = @[@"male", @"male", @"male", @"male", @"male", @"famale", @"famale"];
    NSString *query2 = [NSString stringWithFormat:@"select * from table1 where name = '%@'", [name lastObject]];
    //NSLog(@"query : %@", query2);
    BOOL recordExist = [self recordExistOrNot:query2];
    if (!recordExist) {
        for (int i = number; i < [idd count]; i++)
        {
            NSString *a = [idd objectAtIndex:i];
            NSString *b = [name objectAtIndex:i];
            NSString *c = [age objectAtIndex:i];
            NSString *d = [sex objectAtIndex:i];
            NSString *query = [NSString stringWithFormat:@"insert into table1 (id, name, age, sex) values ('%@', '%@', '%@', '%@')", a, b, c, d];
            NSLog(@"%@", query);
            [self executeQuery:query];
        }
    }
}

I want two things but I not found any thing about it.

First: If in above code one or two of names changed (for example NSArray *name was:

{@"janatan", @"louis", @"john", @"fredrick", @"vein", @"emma", @"shyla"}

how I can edit my table SQLite DB and replace louis & fredrick instead fred & cristiano?)

Second: If in above code one or two names removed (for example NSArray *name was:

{@"janatan", @"john", @"vein", @"emma", @"shyla"}

how I can remove Record the name of the deleted?)

thanks a lot.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
191 views
Welcome To Ask or Share your Answers For Others

1 Answer

for that you can use UPDATE Query.

try like this run for loop with unique identification value and update the values.

UPDATE table1 SET ContactName='newName' WHERE id=uniqu_id

Use this one.

NSString *query = [NSString stringWithFormat:@"UPDATE table1 SET  name='%@' WHERE id='%@' ,[dic objectForKey:@"name"],[dic objectForKey:@"id"]];

EDIT:

NSString *query = [NSString stringWithFormat:@"UPDATE table1 SET  name='%@',age='%@' WHERE id='%@' ,[dic objectForKey:@"name"],[dic objectForKey:@"age"],[dic objectForKey:@"id"]];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...