jeudi 29 janvier 2015

How to insert multiple rows into SQLite database

I have a JSON file that is parsed into an array of 450 dictionaries, each dictionary has 26 key/value pairs. I need these inserted into a database efficiently.


I currently iterate through each dictionary item in the JSON array, make two arrays (one for keys and one for values) and then insert the row into my database



NSString *jsonFile = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/content/formulary/files/%@", jsonToGet]];
NSData *jsonData = [NSData dataWithContentsOfFile:jsonFile];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonError];

NSString *formularyTable = [NSString stringWithFormat:@"formulary_%@", [jsonToGet stringByDeletingPathExtension]];

DataBaseMananger *dataBaseManagerInstance = [DataBaseMananger new];

for (NSDictionary *dict in jsonArray) {

NSMutableArray *allKeys = [NSMutableArray new];
NSMutableArray *allValues = [NSMutableArray new];

allKeys = [NSMutableArray arrayWithArray:[dict allKeys]];

for (NSString *key in allKeys) {

NSString *value = [dict objectForKey:key];
[allValues addObject:value];

}

[dataBaseManagerInstance insertToTable:formularyTable setColumns:allKeys equals:allValues];
}


This is very slow and I need a much faster way. I would prefer to have a method that can accpet the JSON array and then can process ALL of the rows in one 'insert' but I don't know how. I have looked through many questions on here and nothing seems to fit my needs.


Aucun commentaire:

Enregistrer un commentaire