测试已测试 | ✓ |
语言编程语言 | Objective C++Objective C++ |
许可证 | BSD |
发布最后发布 | 2014年12月 |
由 Alexander Dodatko 维护。
依赖于 | |
ESLocale | ~> 1.1 |
ObjcScopedGuard | ~> 1.0 |
ESDatabaseWrapper | ~> 1.0 |
FMDB | ~> 2.0 |
这是一个用于解析简单 *.CSV 文件的库。
由于我们不支持引号值,该库不完全符合 rfc4180。
主要目标是使用最小的内存占用将 *.CSV 数据导入 SQLite 数据库。
License : BSD
该库可以将 CSV 文件导入到 SQLite 表中。我们尽力让它尽可能少地使用内存。
The library does not fully comply to rfc4180 for speed and simplicity.
We do not support quoted values.
以下是 CSV 导入器使用示例
-(void)testImportWithInvalidDefauls
{
NSString* csvPath_ = [ [ NSBundle bundleForClass: [ self class ] ] pathForResource: @"UnixTest3"
ofType: @"csv" ];
NSString* fullDatabasePath = @"1.sqlite";
NSDictionary* schema_ = @{
@"Date" : @"DATETIME",
@"Integer" : @"INTEGER",
@"Name" : @"VARCHAR",
@"Id" : @"VARCHAR",
@"TypeId" : @"INTEGER"
};
NSOrderedSet* primaryKey_ = [ NSOrderedSet orderedSetWithObjects: @"Date", @"Id", @"TypeId", nil ];
CsvDefaultValues* defaults_ = [ CsvDefaultValues new ];
[ defaults_ addDefaultValue: @""
forColumn: @"Name" ];
[ defaults_ addDefaultValue: @"10"
forColumn: @"TypeId" ];
CsvToSqlite* converter_ = [ [ CsvToSqlite alloc ] initWithDatabaseName: fullDatabasePath
dataFileName: csvPath_
databaseSchema: schema_
primaryKey: primaryKey_
defaultValues: defaults_
separatorChar: ';'
commentChar: '#'
lineReader: [ UnixLineReader new ]
dbWrapperClass: [ FMDatabase class ] ];
converter_.csvDateFormat = @"yyyyMMdd";
NSError* error_;
[ converter_ storeDataInTable: @"Campaigns"
error: &error_ ];
XCTAssertNotNil( error_, @"Unexpected error" );
}
列解析是此 CSV 导入器实现中的最大瓶颈。对于某些数据集,可以省略此步骤。
为此,原始的 CSV 行
Date , Id, Visits
2014-01-01, 10, 100500
转换为以下查询
INSERT INTO [TrafficStats] - SQL Insert statement added
( Date, Id, Visits ) - Brackets added
VALUES - SQL keyword added
( '2014-01-01', '10', '100500' ) - Brackets and quotes added
yyyyMMdd
格式的日期我们测量了旧版 iPad2 的基准测试,这是我能测试的较老且速度较慢的型号。我们测量了整个导入过程,包括
以下表格中的数字是10次发布的平均值。请自由使用dodikk/CsvToSqlite-Profiling存储库进行进一步研究。
日期格式 | 格式注释 | 时间 |
---|---|---|
yyyy-MM-dd | ANSI格式 | 11秒 |
yyyyMMdd | "紧凑" ANSI | 17秒 |
其他 | 使用NSDateFormatter | 28秒 |
日期格式 | 格式注释 | 时间 |
---|---|---|
yyyy-MM-dd | ANSI格式 | 20秒 |
yyyyMMdd | "紧凑" ANSI | 32秒 |
其他 | 使用NSDateFormatter | 40秒 |
推荐方法是使用子项目。然而,cocoapods用户欢迎输入pod install CsvToSqlite
命令
Make the library rfc4180 compliant. Start using davedelong / CHCSVParser for better CSV handling