0

I am using following code to insert rows more than 5000 in my ios app. if i am not using line sqlite3_close(dbv); statement i got an error unable to open database. and if i use the statement sqlite3_close(dbv) data insertion taking around 10-15 mins. what can i do to insert record faster without getting error.

-(void)insertrecordIntoTable:(NSString*)SQLStatement
{
    NSString *sqlStr=SQLStatement;
    const char *sql=[sqlStr UTF8String];
     const char* key = [@"StrongPassword" UTF8String];
    sqlite3_stmt *statement1;
    @try {

        int res = sqlite3_open([[self.databaseURL path] UTF8String], &dbv);
        if (res != SQLITE_OK)
            sqlite3_open([[self.databaseURL path] UTF8String], &dbv);

        sqlite3_key(dbv, key, (int)strlen(key));

        if(sqlite3_prepare_v2(dbv, sql, -1, &statement1, nil)==SQLITE_OK)
        {
            NSLog(@"done");
        }
        else
        {
             NSLog(@"the error occurred here is %s ",sqlite3_errmsg(dbv));
        }

        res =sqlite3_step(statement1);

        if(res !=SQLITE_DONE)
          NSLog(@"Error upadating table");

        sqlite3_finalize(statement1);
        sqlite3_close(dbv);
    }
    @catch (NSException *exception) {

    }
    @finally {
        sql=NULL;
        sqlStr=NULL;
        SQLStatement=NULL;
    }
}
SUNIL JOSHI
  • 192
  • 1
  • 12

2 Answers2

1

In addition to Begin Transaction and Commit Transaction i try with opening the database once, leaving the connection open during the entirely of my application, and close the connection on the termination of the application? sqlite3_key is slow by design and the code above is forcing an open/key/close process for every record you insert which will slow down your operating considerably.

SUNIL JOSHI
  • 192
  • 1
  • 12
0

After opening database add this code

sqlite3_exec(mDb, "BEGIN TRANSACTION", NULL, NULL, &errorMessage);

And, before closing database add this code

sqlite3_exec(mDb, "COMMIT TRANSACTION", NULL, NULL, &errorMessage);

For more details check this link-

How to insert 40000 records fast into an sqlite database in an iPad

Community
  • 1
  • 1
Tushar Limaye
  • 106
  • 2
  • 7