Reputation: 1875
I have a .csv-document with a lot of data. It has 71 different columns.
Now I want to import this data to Core Data. But when I create the attributes in xcdatamodel the attributes sorts by its name, and not in the order that I want them to be. So when the .sqlite-file is created, the columns is in the wrong order. I've tried to order them in SQLite-manager, but when I reopen the .sqlite the columns is in the same order as before the change.
Is there a way to re-order the columns in SQLite-manager?
Upvotes: 1
Views: 645
Reputation: 58097
Well, 71 columns is a lot. Since you're using Core Data, I'd suggest trying to denormalize a little bit. Core Data is not a tabular database. That said, I'd suggest keeping track of the current column, and storing the data in an array. Then, you separate the data into NSManagedObjects as necessary.
EDIT:
Let's pretend that your sqlite database had three columns in this order: firstName
, lastName
, middleInitial
. Now imagine that your CSV file had them in a different order, like lastName
, middleInitial
, firstName
. You could reorder the CSV file or the sqlite file, but since you're using Core Data, you can do this another way.
You're going to have an entity with all of your properties. In my example, it's the "Person" entity. So, we go ahead and create an instance of this Person entity.
Person *newPerson = [[Person alloc] initWithEntity:[NSEntityDescription entityForName:@"Person" inManagedObjectContext:context] insertIntoManagedObjectContext:context];
Now, you need to maintain a reference to this Person
instance so that you can access it when parsing your CSV. I recommend Dave DeLong's CHCSVParser library. Essentially, it allows event-based parsing of CSV files.
When you hit a new field, check which field number it is. You can do this by keeping a currentField variable around, resetting it to zero when you hit a new line, and incrementing on every field. Now, when you hit a field, if the number is 0, or the first field, you set newPerson.lastName
. If it's the second field, you assign newPerson.middleInitial
. And, if you hit the third field, you set newPerson.firstName
. The idea is that you can assign persistent properties without touching the sqlite database. If you're using Core Data, this is the way to go.
Upvotes: 1