如何手动在核心数据实体中添加数据 [英] How to add data manually in core data entity

查看:143
本文介绍了如何手动在核心数据实体中添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用 Core Data
我刚刚为该实体创建了一个 Entity Attributes 。我想添加一些行作为数据(你可以说我想在表中添加数据)。

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

早前当我使用 Sqlite ,我将使用 Terminal 添加数据。但是在 Core Data 中,我无法找到手动添加数据的方法。我只是想添加数据到上下文并显示在 UITableView 。我已经经历了 Core Data 的文档,但它不解释如何手动添加数据,虽然它解释了如何以编程方式添加它。但我不需要做它的程序。我想手动做。

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.

推荐答案

我对CoreData相当新,也正在寻找一个解决方案。我很高兴在这里找到一篇文章: http://iphoneinaction.manning.com/iphone_in_action/核心数据,了解如何获取CSV文件并导入您的数据(查找核心数据,第5部分:预填数据)。

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

这里是代码我使用:

-(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:@MyEntityinManagedObjectContext:[self managedObjectContext]];
[myMO setAttrib1:[NSNumber numberWithInt:i + 1]];
[myMO setAttrib2:[dataRows objectAtIndex:i]];
[self saveAction];
}
}

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文件:

CSV file:

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

代码:

- (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];
  }
 }
}

要回答你的评论:不,我想手动添加数据,类似于我们在sqlite通过终端或在sql服务器使用sql查询。下载FireFox并搜索名为 SQLITE MANAGER 的Add-On工具。它将允许您打开任何SQLITE数据库,甚至是由您的应用程序创建的。 SQLITE MANAGER 是SQLITE数据库的GUI,类似于具有较少功能的 MS SQL Server Management Studio 。您可以通过它查看,编辑和添加数据,如果您打算通过Core Data使用您的数据库,我建议您通过此工具不要添加数据。你甚至可以将这个工具用于通常通过Terminal创建的SQLITE数据库(这是我需要的时候,当不需要使用MS SQL时)。

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

这篇关于如何手动在核心数据实体中添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆