如何在iPhone应用程序中将CSV数据动态导入sqlite [英] How to import CSV data to sqlite dynamically in iphone application

查看:61
本文介绍了如何在iPhone应用程序中将CSV数据动态导入sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困住了. 有没有可以将CSV记录直接导入sqlite表的功能? 任何帮助都将是可贵的.

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

推荐答案

下面是执行此操作的粗略代码.请根据您的要求进行一些更改.

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:@","];

//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:@","];

[d setValue:[arr objectAtIndex:0] forKey:@"columnName"]; //将此字典"d"添加到数据库中
[d版本]; //清理.
}
}
[path1版本];
}

请同时参考以下问题的答案.创建自己的自定义CSV解析器有一个很好的解释.

           [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.

从csv文件(iphone SDk)加载数据 [如何导入CSV数据在iPhone应用程序中动态地进行sqlite操作


[2]:如何在iPhone应用程序中将CSV数据动态导入sqlite


我希望这会对您有所帮助.


I hope this will helps you.

这篇关于如何在iPhone应用程序中将CSV数据动态导入sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆