Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add data manually in core data entity

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 data (you can say i want to add data in a table).

Earlier when I was using Sqlite, I would add data using the Terminal. But here in Core Data I am not able to find a way to manually add data. I just want to add data to the Context and display it in a UITableView. I have gone through the the documentation of Core Data but it does not explain how to add data manually, although it explains how I can add it programmatically. But I don't need to do it programmatically. I want to do it manually.

like image 714
pankaj Avatar asked Apr 13 '10 13:04

pankaj


1 Answers

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).

like image 99
RoLYroLLs Avatar answered Oct 25 '22 02:10

RoLYroLLs