I want to insert 40000 records that i get from a web service into a sqlite database in my iPad app.
I wrote the following code, but it takes around 20 minutes, is there a faster way?
- (NSArray *)insertPriceSQLWithPrice:(Price *) price
{
SQLiteManager *dbInfo = [SQLiteManager sharedSQLiteManagerWithDataBaseName:@"codefuel_catalogo.sqlite"];
sqlite3 *database;
NSString *querySQL=[self formatStringQueryInsertWithTable:@"prices_list" andObject:price];
if(sqlite3_open([dbInfo.dataBasePath UTF8String], &database) == SQLITE_OK)
{
sqlite3_stmt * compiledStatement;
const char *query_stmt = [querySQL UTF8String];
int result = sqlite3_prepare_v2(database, query_stmt, -1, &compiledStatement, NULL);
if (result == SQLITE_OK)
{
int success = sqlite3_step(compiledStatement);
NSLog(@"el numero de success es -> %i",success);
if (success == SQLITE_ERROR)
NSLog(@"Error al insertar en la base de datps");
}
else
NSLog(@"Error %@ ERROR!!!!",querySQL);
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
return nil;
}
There are three things that you need to do in order to speed up the insertions:
sqlite3_openoutside the loop. Currently, the loop is not shown, so I assume it is outside your code snippetBEGIN TRANSACTIONandCOMMIT TRANSACTIONcalls – you need to begin transaction before the insertion loop and end it right after the loop is over.formatStringQueryInsertWithTabletruly parameterized – Currently it appears that you are not using prepared statements to their fullest, because despite usingsqlite3_prepare_v2, you have no calls ofsqlite3_bind_XYZin your code.Here is a nice post that shows you how to do all of the above. It is plain C, but it will work fine as part of an Objective C program.