-1

如何在 iPhone 中创建任何应用程序以将 csv 文件导入 sqlite3。请提供任何示例、建议或任何帮助。

4

1 回答 1

0

执行以下步骤将 csv 导入 sqlite3 数据库

  1. 将文件添加到资源文件夹中。(假设文件名是“List.csv”)

  2. 首先获取CSV文件的路径。

    NSString *path = [[NSBundle mainBundle] pathForResource:@"List" ofType:@"csv"];
    
  3. 编写以下方法来解析和插入数据库。

    [self insertIntoDatabaseFromFile:path];
    
  4. 方法如下

    -(void)insertIntoDatabaseFromFile:(NSString*)path
    {
    
     NSString *fileDataString=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
     //Gives u the array with all lines separated by new line.
     NSArray *linesArray=[fileDataString componentsSeparatedByString:@"\r"];
     int k = 0;
     for (id string in linesArray){
    
        //Gives u the number of items in one line separated by comma(,)
        NSString *lineString=[linesArray objectAtIndex:k];
    
        //Gives u the array of all components.
        NSArray *columnArray=[lineString componentsSeparatedByString:@","];
    
       //Get elements from array and create a query to add all into database.
       //Write your code for that.
    
       k++;
    
    }
    
于 2012-12-06T11:53:34.533 回答