1

I am stuck. Is there any function which can import CSV records directly to sqlite table ? Any help will be appreciable.

3
  • You need this to be done programatically ? or just need to import in DB from any csv file with using tools ? If you need to use tool I suggest you to use SQLITE manager AddON Commented Apr 21, 2012 at 6:21
  • I need to be done programatically, as I need to synch data from server to my app and server having the data in CSV files so I have to do programatically. Commented Apr 21, 2012 at 6:28
  • Look at the answers below. You have to write your own logic. Commented Apr 21, 2012 at 6:38

4 Answers 4

2

Here is a rough code to do that. Please do some changes as per your requirement.

-(void)loadCSVData{
    
       NSStringEncoding encoding;    NSString *path1=[[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:filename ofType:@"csv"]  usedEncoding:&encoding error:nil]; //You will get the array of lines    NSArray *messArr=[path1 componentsSeparatedByString:@"\n"];

//Now start to process each single line.    if(messArr)    {        for(int i=0;i<=[messArr count]-1;i++)        {                        NSMutableDictionary *d=[[NSMutableDictionary alloc] init];            NSString *StrValue=[NSString stringWithFormat:@"%@",[messArr objectAtIndex:i]];            StrValue=[StrValue stringByReplacingOccurrencesOfString:@"\"" withString:@""];            StrValue=[StrValue stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];            NSArray *arr=[StrValue componentsSeparatedByString:@","];

/*Add value for each column into dictionary*/

           [d setValue:[arr objectAtIndex:0] forKey:@"columnName"];             //Add this dictionary "d" into database
[d release]; //Cleanup.
       }
   }
[path1 release];    
}

Please also refer the following question's answers. There is a nice explanation of creating your own custom CSV parser.

Load data from csv file (iphone SDk) [How to import CSV data to sqlite dynamically in iphone application


[2]: How to import CSV data to sqlite dynamically in iphone application


I hope this will helps you.

Sign up to request clarification or add additional context in comments.

1 Comment

For me it worked with @"\r" : NSArray *messArr=[path1 componentsSeparatedByString:@"\r"];
1

You can read CSV file using methods like

NSData  *CSVdata = [NSData dataWithContentsOfFile:path];
NSString *CSVString = [CSVdata dataUsingEncoding:NSASCIIStringEncoding];

You need to group data from this string and then can be inserted into DB

1 Comment

UTF Encoding not working well for csv file exctractor. ASCI encoding working fine.
0

If you are asking to do this programatically then you will have to write your own logic for it viz. 1) Read a row (if its a header row create a table with following fields) 2) Insert records to your table with various fields mapped to your columns of particular table.

Else if you are only interested in inserting the records anyhow then you can use SQLiteManager tool for mac or its plugin for your browser

You can take hint from following screengrab (Using SQliteManger for Mac . Open Database--> File-->Import-->other)

enter image description here

Comments

-1

Read this post..Imort csv into sqllite

second reference for you

Hope,this will help you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.