Objective C - 平面文件到核心数据

时间:2011-05-21 19:18:02

标签: objective-c core-data flat-file

我有一个包含100万行的大文件。该文件设置如下:

A1 B1 C1 D1 E1
A2 B2 C2 D2 E2
A3 B3 C3 D3 E3

我想将这些数据读入由CoreData管理的SQLite数据库。

最有效和最有效的方法是什么?

这可以通过使用CoreData& Objective-C或创建一个SQLite数据库,然后将其导入到使用CoreData的项目中。

2 个答案:

答案 0 :(得分:2)

如果这不是你需要动态做的事情,那么我可能会写一些类型的脚本:(1)创建数据库/模式,然后(2)从文件中读取并插入适当的表格。然后我将数据库导入到项目中。

答案 1 :(得分:1)

这是我在iOS应用程序中使用的方法示例,用于加载分隔文本文件(使用管道“|”作为字段分隔符),其中包含有关几种植物和动物物种的数据。该文件存储在应用程序的Documents目录中,因此用户可以通过iTunes通过文档共享来添加/更改它。该文件名为“X.specieslist”,其中X与Core Data管理的sqlite数据库同名。代码检查数据库中是否已存在给定物种(基于“代码”键),并仅导入不存在的物种。

- (void)loadSpecies {

    //get path to species file stored in Documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSPersistentStoreCoordinator *psc = [managedObjectContext persistentStoreCoordinator];
    NSArray *psa = [psc persistentStores];
    NSURL *psurl = [[psa objectAtIndex: 0] URL];
    NSString *filestr = [[psurl lastPathComponent] stringByReplacingOccurrencesOfString: @"sqlite" withString: @"specieslist"] ;
    NSString *filePath = [basePath stringByAppendingPathComponent: filestr];

    //create array with each element containing a single line from the input file
    NSArray *speciesRecords = [[NSString stringWithContentsOfFile: filePath encoding: NSUTF8StringEncoding error: nil] componentsSeparatedByString: @"\n"];

    if (speciesRecords) {
        int speciesAdded = 0;
        int speciesSkipped = 0;
        NSEntityDescription *entity = [NSEntityDescription entityForName: @"Species" inManagedObjectContext: managedObjectContext];
        NSManagedObject *newSpecies;
        NSDictionary *speciesDictionary;
        NSArray *fieldKeys = [[NSArray alloc] initWithObjects: @"code", @"scientificName", @"commonName", @"family", @"lifeform", @"growthForm", @"lifecycle", @"nativity", @"notes", nil];
        NSArray *fieldValues;
        NSArray *existingRecords = [fetchedResultsController fetchedObjects];
        NSPredicate *speciesCodePred;
        NSArray *speciesMatch;

        for (NSString *species in speciesRecords) {
            fieldValues = [species componentsSeparatedByString: @"|"];

            //check if species code already exists
            speciesCodePred = [NSPredicate predicateWithFormat: @"code MATCHES %@", [fieldValues objectAtIndex: 0] ];
            speciesMatch = [existingRecords filteredArrayUsingPredicate: speciesCodePred];

            //only add species not already present
            if ([speciesMatch count] == 0) {
                newSpecies = [[NSManagedObject alloc] initWithEntity: entity insertIntoManagedObjectContext: managedObjectContext];
                speciesDictionary = [NSDictionary dictionaryWithObjects: fieldValues forKeys: fieldKeys];
                [newSpecies setValuesForKeysWithDictionary: speciesDictionary];
                [newSpecies release];
                speciesAdded++;
            }
            else
                speciesSkipped++;
        }
        [fieldKeys release];
        NSString *speciesSkippedString = (speciesSkipped > 0) ? [NSString stringWithFormat: @" and %d species were skipped because they were already present.", speciesSkipped] : @".";

        UIAlertView *av = [[UIAlertView alloc] initWithTitle: @"Species List Import" message: [NSString stringWithFormat: @"%d species were added to the database%@", speciesAdded, speciesSkippedString] delegate: nil cancelButtonTitle: @"OK" otherButtonTitles: nil];
        [av show];
        [av release];
    }

    else {
        UIAlertView *av = [[UIAlertView alloc] initWithTitle: @"Species List Import" message: @"A species list file was not present, no species were imported." delegate: nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [av show];
        [av release];
    }
}