需要从SQLite数据库填充核心数据 [英] Need To Populate Core Data From SQLite Database

查看:213
本文介绍了需要从SQLite数据库填充核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQLite数据库,其中有4个表,我想加载到我的Core Data数据库,以便我可以实现核心数据。任何人都可以帮助我这个?



我准备好了核心数据应用程序,我准备好了SQLite数据库。我需要从SQLite表中拷贝他们在App代表中的核心数据中的4个实体的数据。



我目前正在使用这个代码,

   - (NSMutableArray *)getCPTCodes 
{
sqlite3 * database = CodesDatabase();

NSMutableArray * cptCodes = [[[NSMutableArray alloc] init] autorelease];

NSString * nsquery = [[NSString alloc] initWithFormat:@SELECT code,short_description,medium_description FROM cpt_codes];
const char * query = [nsquery UTF8String];
[nsquery release];

sqlite3_stmt * statement;
int prepareCode =(sqlite3_prepare_v2(database,query,-1,& statement,NULL));
if(prepareCode == SQLITE_OK){
while(sqlite3_step(statement)== SQLITE_ROW)
{
CPTCodes * code = [[CPTCodes alloc] init];
code.code = [NSString stringWithCString:(char *)sqlite3_column_text(statement,0)encoding:NSUTF8StringEncoding];
code.name = [NSString stringWithCString:(char *)sqlite3_column_text(statement,1)encoding:NSUTF8StringEncoding];
code.description = [NSString stringWithCString:(char *)sqlite3_column_text(statement,2)encoding:NSUTF8StringEncoding];
[cptCodes addObject:code];
// NSLog(@代码:%@简短描述:%@ Long描述:%@,hcpcsCode.code,hcpcsCode.name,hcpcsCode.description);
[code release];
}
sqlite3_finalize(statement);
}
return cptCodes;
}

我需要将它添加为数组在核心数据中,使用这个?



insertNewObjectForEntityForName:inManagedObjectContext:

=h2_lin>解决方案

看起来您可以创建一个实体和NSManagedObject子类来代替现有代码中的代码



您的实体将如下所示:

  CPTCode {
code:string
name:string
desc:string //description是NSObject的一个方法,因此您不能将其用作属性名称
}

...然后你可以让Xcode生成一个自定义子类,让它调用 CPTCodeMO

 <$> 

c $ c> CPTCodeMO * code;
while(sqlite3_step(statement)== SQLITE_ROW){
code = [NSEntityDescription insertNewObjectForEntityForName:@CPTCodeinManagedObjectContext:self.managedObjectContext];
code.code = [NSString stringWithCString:(char *)sqlite3_column_text(statement,0)encoding:NSUTF8StringEncoding];
code.name = [NSString stringWithCString:(char *)sqlite3_column_text(statement,1)encoding:NSUTF8StringEncoding];
code.description = [NSString stringWithCString:(char *)sqlite3_column_text(statement,2)encoding:NSUTF8StringEncoding];
}

每次循环都会创建一个新的CPTCodeMO实例,受管对象上下文。创建所有新实例后,您只需保存上下文:

  [self.managedObjectContext save:& saveError]; 

...



当然有些细微差别,但这是基本的想法。



执行一次,您将拥有一个包含现有数据的Core Data SQLite存储文件。然后,您可以在应用程序包中包含该应用程序随附的应用程序或做别的事情。



我通常在Xcode中创建一个小型实用程序项目,只是做这种导入。


I have an SQLite database with 4 tables in it that I want to load into my Core Data database so I can implement core data. Can anyone help me with this?

I have the core data app ready, and I have the SQLite database ready. I need to copy the data from SQLite tables their 4 respective entities in core data in App delegate.

I'm currently using this code, how can I edit it so that instead of adding to array, it creates a new object in MOC?

-(NSMutableArray *) getCPTCodes
{
    sqlite3 *database = CodesDatabase();

    NSMutableArray *cptCodes = [[[NSMutableArray alloc] init] autorelease];

    NSString *nsquery = [[NSString alloc] initWithFormat:@"SELECT code, short_description, medium_description FROM cpt_codes"];
    const char *query = [nsquery UTF8String];
    [nsquery release];

    sqlite3_stmt *statement;
    int prepareCode = (sqlite3_prepare_v2( database, query, -1, &statement, NULL));
    if(prepareCode == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            CPTCodes *code = [[CPTCodes alloc] init];
            code.code = [NSString stringWithCString:(char *)sqlite3_column_text(statement, 0) encoding:NSUTF8StringEncoding];
            code.name = [NSString stringWithCString:(char *)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding];
            code.description = [NSString stringWithCString:(char *)sqlite3_column_text(statement, 2) encoding:NSUTF8StringEncoding];
            [cptCodes addObject:code];
            //            NSLog(@"Code: %@ Short Description: %@ Long Description: %@", hcpcsCode.code, hcpcsCode.name, hcpcsCode.description);
            [code release];
        }
        sqlite3_finalize(statement);
    }
    return cptCodes;
}

Instead of loading that into an array, I need to add it as an object in core data, using this?

insertNewObjectForEntityForName:inManagedObjectContext:

解决方案

It looks like you can create an entity and NSManagedObject subclass to take the place of the Code class in your existing code.

Your entity would look like:

CPTCode{
  code:string
  name:string
  desc:string // "description" is a method of NSObject so you can't use it as a property name
}

... and then you could have Xcode generate a custom subclass, lets call it CPTCodeMO just for clarity.

Then you adapt your existing code:

CPTCodeMO *code;
while (sqlite3_step(statement) == SQLITE_ROW){
  code=[NSEntityDescription insertNewObjectForEntityForName:@"CPTCode" inManagedObjectContext:self.managedObjectContext];
  code.code = [NSString stringWithCString:(char *)sqlite3_column_text(statement, 0) encoding:NSUTF8StringEncoding];
  code.name = [NSString stringWithCString:(char *)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding];
  code.description = [NSString stringWithCString:(char *)sqlite3_column_text(statement, 2) encoding:NSUTF8StringEncoding];
}

Each pass through the loop would create a new CPTCodeMO instances and insert it in the managed object context. When you have created all the new instances you would just save the context:

[self.managedObjectContext save:&saveError];

... and your done.

There are nuances of course but this is the basic idea.

Do that once and you will have a Core Data SQLite store file with the existing data. You can then include that in the app bundle to ship with the app or do something else with it.

I usually create a small utility app project in Xcode just to do this kind of import.

这篇关于需要从SQLite数据库填充核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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