user594161
user594161

Reputation:

Need To Populate Core Data From SQLite Database

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:

Upvotes: 1

Views: 2177

Answers (1)

TechZen
TechZen

Reputation: 64428

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.

Upvotes: 1

Related Questions