2011-01-20 59 views
0

嗨,我使用follwing代碼插入批量數據到數據庫到SQLite數據庫...但它是非常緩慢的...請告知... 我使用正確的方法?批量插入到iPhone的SQLite數據庫

sqlite3 * db1 = nil; db1 = [One2GuideUAppDelegate getNewDBConnection];

sqlite3_stmt *init_statement = nil; 
@try 
{ 


    if(init_statement == nil) 
    { 
     const char *sql = "insert into tblBrand(intBId,intVersion,intLikes,intDontLikes,strName,strDesc,strWebsite,intEst,strFounder,strLogo,strThumbnail) values(?,?,?,?,?,?,?,?,?,?,?)"; 
     if(sqlite3_prepare_v2(db1, sql, -1, &init_statement, NULL) != SQLITE_OK) 
      NSAssert1(0,@"Error: Failed to prepare statement with message '%s'.",sqlite3_errmsg(db1)); 
    } 

    for(int br=0;br<[brands count];br++) 
    { 
     BrandVO *brandVO=[brands objectAtIndex:br ]; 
     NSString *brandID=brandVO.brandID; 
     NSString *brandVer=brandVO.brandVersion; 
     NSString *brandLikes=brandVO.likes; 
     NSString *brandDontLikes=brandVO.dontLikes; 
     NSString *brandName=brandVO.name; 
     NSString *brandDesc=brandVO.description; 
     NSString *brandWeb=brandVO.url; 
     NSString *brandEstb=brandVO.establishedYear; 
     NSString *brandFounder=brandVO.founders; 
     NSString *brandLogo=brandVO.logoURL; 
     NSString *brandthumb=brandVO.thumbnailURL; 





    sqlite3_bind_int(init_statement, 1, [brandID intValue]); 
    sqlite3_bind_int(init_statement, 2, [brandVer intValue]); 
    sqlite3_bind_int(init_statement, 3, [brandLikes intValue]); 
    sqlite3_bind_int(init_statement, 4, [brandDontLikes intValue]); 
    sqlite3_bind_text(init_statement, 5,[brandName UTF8String],-1,SQLITE_TRANSIENT); 
    sqlite3_bind_text(init_statement, 6,[brandDesc UTF8String],-1,SQLITE_TRANSIENT); 
    sqlite3_bind_text(init_statement, 7,[brandWeb UTF8String],-1,SQLITE_TRANSIENT); 
    sqlite3_bind_int(init_statement, 8,[brandEstb intValue]); 
    sqlite3_bind_text(init_statement, 9,[brandFounder UTF8String],-1,SQLITE_TRANSIENT); 
    sqlite3_bind_text(init_statement, 10,[brandLogo UTF8String],-1,SQLITE_TRANSIENT); 
    sqlite3_bind_text(init_statement, 11,[brandthumb UTF8String],-1,SQLITE_TRANSIENT); 


    if(SQLITE_DONE != sqlite3_step(init_statement)) 
     NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(db1)); 

    sqlite3_reset(init_statement); 
     } 

} 

@catch (NSException *ex) 
{ 
    @throw ex; 
} 
@finally 
{ 
    if(init_statement)sqlite3_finalize(init_statement); 
    sqlite3_close(db1); 
} 

感謝,

回答

3

環繞你插入語句與交易和BEGIN TRANSACTIONEND TRANSACTION。這通常會加速批量插入。

+0

這是一個很好的幫助! – devaditya 2011-01-20 09:05:59