简体   繁体   中英

Import csv file into SQLite

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

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

How can I do that?

i done with below code you need to just impliment that:-

 -(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
   {
   }
   } 

You can do as this way,

First you need to add your csv file in bundle.

Then you can call this method where you want to add data in 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
}
}

You should better import data in Linux. And in Disk Operation System after into the SQLite,enter

separator",";

import choose your csv path xyz. When you create table named xyz , the name should be agree with your csv files .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM