how can I import csv to s开发者_JAVA技巧qlite (core data for iphopne)
I have tried using SQLite manager, but it imports the csv to a new table, also I need to import some dates,
so how to import the data to my sqlite database? I have 3 entitys with different properties, and in the csv I have all the values in one csv (so I could format it or change it as needed), but how to impor it?
also what is the date format that coredata likes?
thanks in advance!
I assume you have setup your CoreData and that one is running fine. You don't really want to work directly with the sqlite DB of core-data. It is possible, but also a bit messy.
- Use one of the CSV scanners floating around to read your CSV data into fields.
- Map the CSV fields to your entities and to their attributes as needed. You might want to use the CSV header to verify that your mapping CSV-column-to-attribute is ok.
- Loop through the rows of the CSV and update your entities row by row.
- Depending on data volume you might want to save your context at regular intervals.
Core-Data likes NSDate. Whatever the CSV file uses in the data column you are best off converting the CSV value into a NSDate. Using NSDate in your App will reduce the number of headaches later.
I would suggest, first create sqlite Database inside application using Core Data, then use database created by Core data to import csv data.
you can use the command line commands for importing data in the sqlite file.
Add the sqlite file to project.
replace and add this code inside the persistentStoreCoordinator method..
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"yourDatabase.sqlite"];
/*
Set up the store.
For the sake of illustration, provide a pre-populated default store.
*/
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn’t exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"yourDatabase" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
Change the method
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
精彩评论