Whats the best way to populate a SQLite or Coredata database on the iPHone/iPad?

Discussion in 'iOS Programming' started by dmelgar, Aug 21, 2010.

  1. dmelgar macrumors 68000

    Apr 29, 2005
    Whats the best way to populate a sqlite or coredata database on the iPHone/iPad?
    Is there a way to import a csv file into sqlite on the iphone?
    Is there some other way typically used to populate the database?

  2. jackal123uk macrumors member

    Jan 13, 2007
    Looking for the same answer myself - populating the core data side of things that is.

    I've got static data set for use in an app, it's currently sitting in an SQlite database (imported from cvs). In previous apps I've just interfaced the SQlite database directly, copied it to a mutable array and from there populated a tableview etc.

    After reading around I feel I should be making the switch to core data but haven't seen a easy way to import an existing SQLite database - I did find a python script for such a conversion but would prefer something a little more user friendly.

    Considering there'll be no additions, alterations or deletions to the database, is it really worth switching to core data?
  3. Kranchammer macrumors member

    Mar 26, 2010
    For using a pre-populated sqlite db:
    I use, in general, the steps outline here:
  4. jackal123uk macrumors member

    Jan 13, 2007
  5. SplitIt! macrumors newbie


    Jul 10, 2012

        // Database seeding is configured as a copied target of the main application. There are only two differences
        // between the main application target and the 'Generate Seed Database' target:
        //  1) RESTKIT_GENERATE_SEED_DB is defined in the 'Preprocessor Macros' section of the build setting for the target
        //      This is what triggers the conditional compilation to cause the seed database to be built
        //  2) Source JSON files are added to the 'Generate Seed Database' target to be copied into the bundle. This is required
        //      so that the object seeder can find the files when run in the simulator.
        RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelInfo);
        RKLogConfigureByName("RestKit/CoreData", RKLogLevelTrace);
        RKManagedObjectSeeder *seeder = [RKManagedObjectSeeder objectSeederWithObjectManager:objectManager];
        // Seed the database with instances of RKTStatus from a snapshot of the RestKit Twitter timeline
        [seeder seedObjectsFromFile:@"restkit.json" withObjectMapping:statusMapping];
        // Seed the database with RKTUser objects. The class will be inferred via element registration
        [seeder seedObjectsFromFiles:@"users.json", nil];
        // Finalize the seeding operation and output a helpful informational message
        [seeder finalizeSeedingAndExit];
        // NOTE: If all of your mapped objects use keyPath -> objectMapping registration, you can perform seeding in one line of code:
        // [RKManagedObjectSeeder generateSeedDatabaseWithObjectManager:objectManager fromFiles:@"users.json", nil];
    in https://github.com/RestKit/RestKit/...witterCoreData/Classes/RKTwitterAppDelegate.m

    Even if you do not use RestKit for communicating with a remote webserver, it's CoreData and ObjectMapping function will be useful.

    As a bonus, if you decide to turn you app in to a cloud based app, you would have already done all the hard work.
  6. jackal123uk macrumors member

    Jan 13, 2007
    Definitely the way ahead and surprisingly easy. Anyone else who's having trouble should give check out the link;basically create your own data model in xcode to match your existing database (an entity for each table each with relevant attributes to match the fields of each table - haven't quite got my head around the relationships bit yet but it's early days) and run in simulator to create the correctly formatted database. Navigate to the simulator filesystem and copy out the database, open it up with your editor of choice and import away. Copy the finished article back into you project's resources and your away.

    Thanks again for the help.

Share This Page