4

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?

yole
  • 92,896
  • 20
  • 260
  • 197
Curious_k.shree
  • 990
  • 2
  • 18
  • 37
  • Possible duplicate of [Import CSV to SQLite](https://stackoverflow.com/questions/14947916/import-csv-to-sqlite) – Dr. X Aug 17 '17 at 12:01

3 Answers3

3

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
   {
   }
   } 
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
3

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
}
}
Nikunj
  • 987
  • 11
  • 25
  • 1
    Sir is there any way throgh which i can directly make changes in sqlite manager? – Curious_k.shree Sep 25 '12 at 11:33
  • Means not getting clearly, you want to edit entries which already exists, OR want to import csv data directy into DB manually ? – Nikunj Sep 25 '12 at 11:49
  • You can directly also import csv file in your database, You can check it with addons in mozilla SQlite manager, open it than Tap on DataBase => Import. Here you will get option to import csv file – Nikunj Sep 25 '12 at 12:05
  • What you've passed for `usedEncoding:` &encoding ? @Nikunj – Hemang Sep 22 '15 at 10:14
0

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

separator",";

importchoose your csv pathxyz. When you create table named xyz, the name should be agree with your csv files .

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Toygirl
  • 79
  • 7