Iphone 将csv文件导入SQLite
我在SQLite中有一个数据库和一个名为xyz的空表。我想将csv文件中的数据导入该表 现在,当我尝试导入csv文件时,它要求我将其导入主表,但我想将数据导入xyz表Iphone 将csv文件导入SQLite,iphone,objective-c,sqlite,csv,import,Iphone,Objective C,Sqlite,Csv,Import,我在SQLite中有一个数据库和一个名为xyz的空表。我想将csv文件中的数据导入该表 现在,当我尝试导入csv文件时,它要求我将其导入主表,但我想将数据导入xyz表 我怎样才能做到这一点呢?我使用了下面的代码,您只需实现它:- -(void)restore{ @try { NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@%@?userid=%@",kHOSTPATH,kCSVLIST,[[cntrAp
我怎样才能做到这一点呢?我使用了下面的代码,您只需实现它:-
-(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
{
}
}
-(无效)还原{
@试一试{
NSURL*url=[NSURL URLWithString:[NSString stringWithFormat:@“%@%@?userid=%@”,khospath,kCSVLIST,[[cntrAppDelegate setServerDetails]valueForKey:@“kUSERID”]];
NSMutableURLRequest*requestMutable=[[NSMutableURLRequest alloc]init];
[requestMutable setURL:url];
NSError*error=[[NSError alloc]init];
NSHTTPURLResponse*响应=nil;
NSData*urlData=[NSURLConnection sendSynchronousRequest:requestMutable returningResponse:&响应错误:&错误];
NSLog(@“响应代码:%d”,[Response statusCode]);
如果([response statusCode]>=200&&[response statusCode]您可以这样做
首先,您需要在bundle中添加csv文件
然后,您可以在希望从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
}
}
-(无效)加载CSVDATA{
NSString*path1=[[NSString alloc]INITWITHCONTENTSOFILE:[[NSBundle mainBundle]pathForResource:@“CSV”类型的“您的CSV文件名”]使用编码:&编码错误:nil];
NSArray*messArr=[path1组件由字符串分隔:@“\n”];
国际单项体育联合会(messArr)
{
对于(inti=1;i您最好在Linux中导入数据。在磁盘操作系统中,在进入SQLite之后,输入
separator",";
导入选择您的csv路径
xyz。当您创建名为xyz
的表时,该名称应与您的csv文件一致。Sir是否有任何方法可以直接在sqlite管理器中进行更改?如果您想从csv导入数据,必须首先从csv获取数据,然后使用insert quirySi将这些数据存储到sqlite中有什么方法可以直接在sqlite管理器中进行更改吗?意思是不清楚,您想要编辑已经存在的条目,或者想要手动将csv数据直接导入DB?您也可以直接在数据库中导入csv文件,您可以在mozilla sqlite管理器中使用插件检查它,打开它,然后点击数据库=>导入。在这里,您将获得导入csv文件的选项您为usedEncoding:
&encoding传递的内容?@nikunjuncy可能重复