如何在iPhone中创建任何应用程序以将csv文件导入sqlite3。请提供任何示例,建议或任何帮助。
答案 0 :(得分:0)
执行以下步骤将csv导入sqlite3数据库
将文件添加到资源文件夹中。(假设文件名为“List.csv”)
首先获取CSV文件的路径。
NSString *path = [[NSBundle mainBundle] pathForResource:@"List" ofType:@"csv"];
编写以下方法来解析并插入数据库。
[self insertIntoDatabaseFromFile:path];
方法如下
-(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++;
}