samedi 19 septembre 2015

iphone - improve INSERT into sqlite

In my app, I need to INSERT multiple times some entry. Each time, I am using this function:

- (int)  insertFunction:(NSString *)stringa{
    NSDate * start = [NSDate date];

    sqlite3_stmt    *statement;
    NSString *file = [self getWritableDBPath];

    if (sqlite3_open([file UTF8String] , &_database) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat:@"%@",stringa];

        const char *insert_stmt = [insertSQL UTF8String];

        sqlite3_prepare_v2(_database, insert_stmt, -1, &statement, NULL);

        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            //NSLog(@"Contact added");

        } else {
            NSLog(@"Failed to add contact");
        }

        sqlite3_finalize(statement);
    }
    sqlite3_close(_database);
    int row_id = (int)sqlite3_last_insert_rowid(_database);

    NSLog(@"SINGLE INSERT took: %f", -[start timeIntervalSinceNow]);

    return row_id;

}

that took about 0.020 second, and make my app frezing about 5 second. What I can do to improve that time? For example, avoiding to open the data base each time? In this case how?

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire