Read large file into sqlite table in objective-C on iPhone

Posted by James Testa on Stack Overflow See other posts from Stack Overflow or by James Testa
Published on 2009-07-31T18:04:58Z Indexed on 2010/03/14 20:05 UTC
Read the original article Hit count: 464

Filed under:
|
|
|

I have a 2 MB file, not too large, that I'd like to put into an sqlite database so that I can search it. There are about 30K entries that are in CSV format, with six fields per line. My understanding is that sqlite on the iPhone can handle a database of this size.

I have taken a few approaches but they have all been slow > 30 s. I've tried:

1) Using C code to read the file and parse the fields into arrays.

2) Using the following Objective-C code to parse the file and put it into directly into the sqlite database:

NSString *file_text = [NSString stringWithContentsOfFile: filePath usedEncoding: NULL error: NULL];

NSArray  *lineArray = [file_text componentsSeparatedByString:@"\n"];

for(int k = 0; k < [lineArray count]; k++){
	NSArray *parts = [[lineArray objectAtIndex:k] componentsSeparatedByString: @","];

	NSString *field0       = [parts objectAtIndex:0];
	NSString *field2       = [parts objectAtIndex:2];
	NSString *field3       = [parts objectAtIndex:3];

NSString *loadSQLi = [[NSString alloc] initWithFormat: @"INSERT INTO TABLE (TABLE, FIELD0, FIELD2, FIELD3) VALUES ('%@', '%@', '%@');",field0, field2, field3];


	if (sqlite3_exec (db_table, [loadSQLi  UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) {
		sqlite3_close(db_table);
		NSAssert1(0, @"Error loading table: %s", errorMsg);
	}

Am I missing something? Does anyone know of a fast way to get the file into a database?

Or is it possible to translate the file into a sqlite format that can be read directly into sqlite?

Or should I turn the file into a plist and load it into a Dictionary? Unfortunately I need to search on two of the fields, and I think a Dictionary can only have one key?

Jim

© Stack Overflow or respective owner

Related posts about sqlite

Related posts about iphone