How to add data manually in core data entity

前端 未结 4 1608
滥情空心
滥情空心 2021-02-03 13:04

I am working on Core Data for the first time. I have just created an Entity and Attributes for that entity. I want to add some rows as da

4条回答
  •  没有蜡笔的小新
    2021-02-03 13:30

    I am fairly new to CoreData as well and was looking for a solution for this too. I was happy to find a an article here: http://iphoneinaction.manning.com/iphone_in_action/core-data on how to get a CSV file and import your data (look for Core Data, Part 5: Prefilling Data).

    Here is the code I use:

    -(void)addData
    {
      NSString *paths = [[NSBundle mainBundle] resourcePath];
      NSString *bundlePath = [paths stringByAppendingPathComponent:@"file.csv"];
      NSString *dataFile = [[NSString alloc] initWithContentsOfFile:bundlePath];
      NSArray *dataRows = [dataFile componentsSeparatedByString:@"\n"];
      [dataFile release];
      MyEntity *myMO;

    for (int i = 0 ; i < [dataRows count] ; i++) { NSLog(@"Added: %d",i); myMO = (MyEntity *)[NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext:[self managedObjectContext]]; [myMO setAttrib1:[NSNumber numberWithInt:i+1]]; [myMO setAttrib2:[dataRows objectAtIndex:i]]; [self saveAction]; } }

    I only had one column so I didn't need all the code. Here is the code from the article. If it doesn't make sense, try looking at the article.

    CSV file:

    1,A$20,Australian Dollars,20,aussie-20.png
    2,R$20,Brazilian Reals,20,brasil-20.png

    Code:

    - (void)setupCards {
     NSString *paths = [[NSBundle mainBundle] resourcePath];
     NSString *bundlePath = [paths stringByAppendingPathComponent:@"cards.csv"];
     NSString *dataFile = [[NSString alloc] initWithContentsOfFile:bundlePath];
     NSArray *dataRows = [dataFile componentsSeparatedByString:@"\n"];
     [dataFile release];
     Card *card;
     for (int i = 0 ; i < [dataRows count] ; i++)
     {
      NSArray *dataElements = [[dataRows objectAtIndex:i] componentsSeparatedByString:@","];
      if ([dataElements count] >= 4)
      {
       card = (Card *)[NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:[self managedObjectContext]];
       [card setId:[NSNumber numberWithInt:i]];
       [card setName:[dataElements objectAtIndex:1]];
       [card setType:[dataElements objectAtIndex:2]];
       [card setWorth:[NSNumber numberWithInt:
       [[dataElements objectAtIndex:3] intValue]]];
       [card setImages:[NSSet setWithObject:
       [self setupCardPic:[dataElements objectAtIndex:4]]]];
       [self saveAction:self];
      }
     }
    }


    To answer your comment of:no i want to add data manually, similar to what we do in sqlite through terminal or in sql server using sql query. Download FireFox and search for an Add-On tool named SQLITE MANAGER. It will allow you to open any SQLITE database, even those created by your app. SQLITE MANAGER is a GUI for SQLITE databases, similar to MS SQL Server Management Studio with less features. You can view, edit and add data through it, ALTHOUGH I recommend AGAINST ADDING data through this tool, if you intend to use your database through Core Data. You can even use this tool for SQLITE databases you'll usually create through Terminal (it's what I do when I need to, and when not needing to use MS SQL).

提交回复
热议问题