CsvToSqlite Class Reference
Inherits from | NSObject |
Declared in | CsvToSqlite.h CsvToSqlite.mm |
Overview
A class that imports a downloaded CSV file to a new table of the SQLite database. Main Features :
- SQL schema validation
- Both Windows (CR,LF) and Unix (LF) line endings supported
- Custom date formats support
- Optimized for ANSI formatted dates
- Optional columns support
All methods are performed synchronously. Make sure to not invoke them on the main thread.
Tasks
-
databaseName
property -
dataFileName
property -
schema
property -
primaryKey
property -
defaultValues
property -
csvDateFormat
property -
onCommentCallback
property -
– dbWrapper
-
– initWithDatabaseName:dataFileName:databaseSchema:primaryKey:
-
– initWithDatabaseName:dataFileName:databaseSchema:primaryKey:defaultValues:
-
– initWithDatabaseName:dataFileName:databaseSchema:primaryKey:defaultValues:lineEndingStyle:recordSeparatorChar:recordCommentChar:
-
– storeDataInTable:error:
Properties
csvDateFormat
Date format for CSV columns.
@property (nonatomic, readwrite) NSString *csvDateFormat
Discussion
Warning : It must be set before importing. Otherwise you’ll get a crash
Declared In
CsvToSqlite.h
dataFileName
A value from the initializer. Full path to the CSV file to import from.
@property (nonatomic, readonly) NSString *dataFileName
Declared In
CsvToSqlite.h
databaseName
A value from the initializer. Full path to the SQLite database for writing.
@property (nonatomic, readonly) NSString *databaseName
Declared In
CsvToSqlite.h
defaultValues
A value from the initializer.
@property (nonatomic, readonly) CsvDefaultValues *defaultValues
Discussion
Default values for gaps in the CSV content.
Declared In
CsvToSqlite.h
onCommentCallback
A block for handling non-standard CSV comments.
@property (nonatomic, copy) CSVOnCommentCallback onCommentCallback
Declared In
CsvToSqlite.h
Instance Methods
dbWrapper
Actual database engine capable of executing raw SQL queries. Used mostly for unit testing.
- (id)dbWrapper
Declared In
CsvToSqlite.h
initWithDatabaseName:dataFileName:databaseSchema:primaryKey:
A legacy initializer.
- (id)initWithDatabaseName:(NSString *)databaseName_ dataFileName:(NSString *)dataFileName_ databaseSchema:(NSDictionary *)schema_ primaryKey:(NSOrderedSet *)primaryKey_
Parameters
- databaseName_
Full path to the SQLite database for writing. For example,
NSString* databaseName_ = @“/tmp/1.sqlite”;
- dataFileName_
Full path to the CSV file to import from. For example,
NSString* dataFileName_ = [ [ NSBundle mainBundle ] pathForResource: @“1” ofType: @“csv” ];
- schema_
A dictionary of column names and their types. Column names must match those in the CSV file.
NSDictionary* schema_ = @{ @“Date” : @“DATETIME”, @“Integer” : @“INTEGER”, @“Name” : @“VARCHAR”, @“Id” : @“VARCHAR”, @“TypeId” : @“INTEGER” };
- primaryKey_
An ordered set of columns for the SQL primary key constraint.
NSOrderedSet* primaryKey_ = [ NSOrderedSet orderedSetWithObjects: @“Date”, @“Id”, @“TypeId”, nil ];
Return Value
A properly initialized CsvToSqlite instance.
Declared In
CsvToSqlite.h
initWithDatabaseName:dataFileName:databaseSchema:primaryKey:defaultValues:
A legacy initializer.
- (id)initWithDatabaseName:(NSString *)databaseName_ dataFileName:(NSString *)dataFileName_ databaseSchema:(NSDictionary *)schema_ primaryKey:(NSOrderedSet *)primaryKey_ defaultValues:(CsvDefaultValues *)defaults_
Parameters
- databaseName_
Full path to the SQLite database for writing. For example,
NSString* databaseName_ = @“/tmp/1.sqlite”;
- dataFileName_
Full path to the CSV file to import from. For example,
NSString* dataFileName_ = [ [ NSBundle mainBundle ] pathForResource: @“1” ofType: @“csv” ];
- schema_
A dictionary of column names and their types. Column names must match those in the CSV file.
NSDictionary* schema_ = @{ @“Date” : @“DATETIME”, @“Integer” : @“INTEGER”, @“Name” : @“VARCHAR”, @“Id” : @“VARCHAR”, @“TypeId” : @“INTEGER” };
- primaryKey_
An ordered set of columns for the SQL primary key constraint.
NSOrderedSet* primaryKey_ = [ NSOrderedSet orderedSetWithObjects: @“Date”, @“Id”, @“TypeId”, nil ];
- defaults_
Sometimes CSV files have gaps in the content. You should specify some defaults for them unless you intentionally want to receive importing errors.
Return Value
A properly initialized CsvToSqlite instance.
Declared In
CsvToSqlite.h
initWithDatabaseName:dataFileName:databaseSchema:primaryKey:defaultValues:lineEndingStyle:recordSeparatorChar:recordCommentChar:
A designated initializer
- (id)initWithDatabaseName:(NSString *)databaseName_ dataFileName:(NSString *)dataFileName_ databaseSchema:(NSDictionary *)schema_ primaryKey:(NSOrderedSet *)primaryKey_ defaultValues:(CsvDefaultValues *)defaults_ lineEndingStyle:(CsvLineEndings)lineEndingStyle_ recordSeparatorChar:(char)separatorChar_ recordCommentChar:(char)commentChar_
Parameters
- databaseName_
Full path to the SQLite database for writing. For example,
NSString* databaseName_ = @“/tmp/1.sqlite”;
- dataFileName_
Full path to the CSV file to import from. For example,
NSString* dataFileName_ = [ [ NSBundle mainBundle ] pathForResource: @“1” ofType: @“csv” ];
- schema_
A dictionary of column names and their types. Column names must match those in the CSV file.
NSDictionary* schema_ = @{ @“Date” : @“DATETIME”, @“Integer” : @“INTEGER”, @“Name” : @“VARCHAR”, @“Id” : @“VARCHAR”, @“TypeId” : @“INTEGER” };
- primaryKey_
An ordered set of columns for the SQL primary key constraint.
NSOrderedSet* primaryKey_ = [ NSOrderedSet orderedSetWithObjects: @“Date”, @“Id”, @“TypeId”, nil ];
- defaults_
Sometimes CSV files have gaps in the content. You should specify some defaults for them unless you intentionally want to receive importing errors.
- lineEndingStyle_
Line ending symbol. Available options :
Platform Line Ending Windows CR LF Unix, Mac OS X LF Mac (PPC) CR
- separatorChar_
Record separator. Usually it is a comma “,”
- commentChar_
Some applications use a non-standard CSV extension to store additional data such as timestamps.
LastModified=06/10/2013 11:37:05
If the file conforms RFC 4180 just pass zero.
Return Value
A properly initialized CsvToSqlite instance.
Declared In
CsvToSqlite.h
storeDataInTable:error:
This method performs importing action.
- (BOOL)storeDataInTable:(NSString *)tableName_ error:(NSError **)error_
Parameters
- tableName_
Name of the table to import to.
- error_
NSError out parameter. Do not pass NULL.
Return Value
YES for successfull input. NO in case of
- Schema validation
- Inconsistent contents of the file
An error object will be written to the “error_” pointer.
Declared In
CsvToSqlite.h