PDA

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




dmelgar
Aug 21, 2010, 12:42 PM
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?

Thanks



jackal123uk
Aug 27, 2012, 04:31 PM
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?

Kranchammer
Aug 27, 2012, 06:40 PM
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?

For using a pre-populated sqlite db:
I use, in general, the steps outline here:
http://ablogontech.wordpress.com/2009/07/13/using-a-pre-populated-sqlite-database-with-core-data-on-iphone-os-3-0/

jackal123uk
Aug 28, 2012, 11:18 AM
For using a pre-populated sqlite db:
I use, in general, the steps outline here:
http://ablogontech.wordpress.com/2009/07/13/using-a-pre-populated-sqlite-database-with-core-data-on-iphone-os-3-0/

Thanks for the heads up - looks just like what I'm after. Gonna give it a go tonight after work.

SplitIt!
Aug 28, 2012, 02:15 PM
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?

Thanks

Checkout:
https://github.com/RestKit/RestKit/tree/master/Examples/RKTwitterCoreData

Specifically:

// 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.
#ifdef RESTKIT_GENERATE_SEED_DB
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];
#endif

in https://github.com/RestKit/RestKit/blob/master/Examples/RKTwitterCoreData/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.

jackal123uk
Aug 29, 2012, 06:22 AM
For using a pre-populated sqlite db:
I use, in general, the steps outline here:
http://ablogontech.wordpress.com/2009/07/13/using-a-pre-populated-sqlite-database-with-core-data-on-iphone-os-3-0/

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.

gummycat
Aug 29, 2012, 06:58 AM
http://www.raywenderlich.com/12170/core-data-tutorial-how-to-preloadimport-existing-data-updated