批量插入到iphone sqlite数据库中
您好,我正在使用下面的代码将大量数据从数组插入sqlite db…但速度非常慢…请告知。。。 我是否使用了正确的方法 sqlite3*db1=nil; db1=[One2GuideUAppDelegate getNewDBConnection]批量插入到iphone sqlite数据库中,iphone,sqlite,Iphone,Sqlite,您好,我正在使用下面的代码将大量数据从数组插入sqlite db…但速度非常慢…请告知。。。 我是否使用了正确的方法 sqlite3*db1=nil; db1=[One2GuideUAppDelegate getNewDBConnection] sqlite3_stmt *init_statement = nil; @try { if(init_statement == nil) { const char *sql = "insert into tblB
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);
}
sqlite3\u stmt*init\u语句=nil;
@试一试
{
if(init_语句==nil)
{
const char*sql=“插入tblBrand(intBId、intVersion、intLikes、intDontLikes、strName、strDesc、strWebsite、intEst、strFounder、strLogo、strThumbnail)值(?、、?、?、?、?、?、?)”;
if(sqlite3\u prepare\u v2(db1,sql,-1,&init\u语句,NULL)!=SQLITE\u OK)
NSAssert1(0,@“错误:无法准备包含消息“%s”的语句”,sqlite3_errmsg(db1));
}
对于(int br=0;br使用BEGIN TRANSACTION
和END TRANSACTION
在事务中包装insert语句。这通常会大大加快批量插入速度。使用BEGIN TRANSACTION
和END TRANSACTION
在事务中包装insert语句。这通常会大大加快批量插入速度。我想我们不能使用相同的(开始和结束事务)在获取数据时…我在从数据库获取数据时遇到了类似的速度问题…请提供建议…检查您是否为正在查询的列创建了索引。我尚未为我指定的列创建任何索引…我猜这是在创建数据库时完成的…如何操作?请参阅或使用GUI工具,如SQLite Manager或Base(有几个)。我想我们不能使用相同的(开始和结束事务)在获取数据时…我在从数据库获取数据时遇到了类似的速度问题…请提供建议…检查您是否为正在查询的列创建了索引。我尚未为我指定的列创建任何索引…我猜这是在创建数据库时完成的…如何操作?请参阅或使用GUI工具,如SQLite Manager或Base(有几种)。