-1

iPhoneでcsvファイルをsqlite3にインポートするためのアプリケーションを作成する方法。例、提案、またはヘルプをお願いします。

4

1 に答える 1

0

次の手順を実行して、csv を sqlite3 データベースにインポートします。

  1. ファイルを resources フォルダーに追加します。 (ファイルの名前は「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 に答える