您好我正在使用以下代码将数据从数组插入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);
}
谢谢,
答案 0 :(得分:3)
在与BEGIN TRANSACTION
和END TRANSACTION
的交易中包含您的插入语句。这通常可以大量加快批量插入。