Import csv file to SQLite

I have a database in SQLite and a table called xyz that is empty. I want to import data from a csv file into this table.

Now when I try to import the csv file, it asks me to import it into the main table, but I want to import the data into the xyz table.

How can i do this?

+4
source share
3 answers

You can do so

First you need to add the csv file to the package.

Then you can call this method where you want to add data to the database from csv

-(void)loadCSVData{ NSString *path1=[[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"YOUR_CSV_FILENAME" ofType:@"csv"] usedEncoding:&encoding error:nil]; NSArray *messArr=[path1 componentsSeparatedByString:@"\n"]; if(messArr) { for(int i=1;i<=[messArr count]-2;i++) { NSMutableDictionary *d=[[NSMutableDictionary alloc] init]; NSString *StrValue=[NSString stringWithFormat:@"%@",[messArr objectAtIndex:i]]; StrValue=[StrValue stringByReplacingOccurrencesOfString:@"\"" withString:@""]; StrValue=[StrValue stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // Here give whatever saperator you need to saperate data NSArray *arr=[StrValue componentsSeparatedByString:@","]; [d setValue:[arr objectAtIndex:0] forKey:@"YOUR_TABLE_FIELD_1"]; [d setValue:[arr objectAtIndex:1] forKey:@"YOUR_TABLE_FIELD_2"]; [d setValue:[arr objectAtIndex:2] forKey:@"YOUR_TABLE_FIELD_3"]; //Here add logic to insert row in your database table } } 
+3
source

i made using the code below, you just need to put into action the following: -

  -(void)restore{ @try { NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@%@?userid=%@",kHOSTPATH,kCSVLIST,[[cntrAppDelegate setServerDetails] valueForKey:@"kUSERID"]]]; NSMutableURLRequest *requestMutable = [[NSMutableURLRequest alloc] init]; [requestMutable setURL:url]; NSError *error = [[NSError alloc] init]; NSHTTPURLResponse *response = nil; NSData *urlData=[NSURLConnection sendSynchronousRequest:requestMutable returningResponse:&response error:&error]; NSLog(@"Response code: %d", [response statusCode]); if ([response statusCode] >=200 && [response statusCode] <300) { NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; NSLog(@"Response ==> %@", responseData); SBJsonParser *jsonParser = [SBJsonParser new]; NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil]; NSLog(@"--------=========%@============-------------",jsonData); NSDictionary *dic = [jsonData objectForKey:@"Root"]; NSLog(@"---------------------ROOT VALUE IS %@",dic); NSLog(@"----------------COUNT IS %d",[dic count]); for (int i = 0; i < [dic count]; i++) { NSString *str = [[dic valueForKey:@"CSV_File"]objectAtIndex:i]; NSLog(@"STR IS %@",str); [self.arrListOfCSV addObject:str]; } if ([jsonData valueForKey:@"Root"] == 0) { } else { } } 
+3
source

You are better off importing data into Linux. And in the Disk Operation System after SQLite, type

 separator","; 

import choose your csv path hoog. When you create a table called xyz , the name should match your csv files.

0
source

Source: https://habr.com/ru/post/1436116/


All Articles