Okay,
I am a noob and want to get a array from my server and insert it into a SQLite database table. My code is below. Please help!
- (void)getAlbums {
// Get Albums
NSString *userId;
userId = @"1";
NSString *post =[NSString stringWithFormat:@"userid=%@", userId];
NSString *hostStr = @"********************************************?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
//Create Array
NSArray *myWords = [serverOutput componentsSeparatedByString:@" "];
// Output server response
NSLog(serverOutput);
//Initialize the array.
NSMutableArray *listOfItems = [[NSMutableArray alloc] init];
listOfItems = [[NSArray arrayWithObjects: myWords, nil] retain];
NSString *test;
while (*test in listOfItems) {
[sqlite executeNonQuery:@"INSERT INTO photo_albums VALUES (?, ?);", variableOne, variableTwo];
}
}
there is more than one issue in this code.
this will leak in the next line because you reassign another item to the variable. Change it into:
NSArray *listOfItems;this does not what you think it does. It will initialize an array with the array myWords as a “member”, it won’t add the objects from myWords.
But why does this line exist anyway? myWords is already an array. And the retain which doesn’t get released would be another leak.
this is just wrong. use:
where the f do you get variableOne and variableTwo from? And why do you loop through your listOfItems if you don’t use the NSString named
test?no problem with the closing bracket.
Oh and serverOutput doesn’t get released too, so there is another leak. And I’m pretty sure that the encoding isn’t ascii.
Start over with the iOS 101, learn the basics and ignore sqlite for the next 2 weeks.