ios – 如何将40000条记录快速插入iPad中的sqlite数据库

ios – 如何将40000条记录快速插入iPad中的sqlite数据库,第1张

概述我想在我的iPad应用程序中将从Web服务获取的40000条记录插入到sqlite数据库中. 我写了下面的代码,但它需要大约20分钟,有更快的方法吗? - (NSArray *)insertPriceSQLWithPrice:(Price *) price{SQLiteManager *dbInfo = [SQLiteManager sharedSQLiteManagerWithDataBa 我想在我的iPad应用程序中将从Web服务获取的40000条记录插入到sqlite数据库中.

我写了下面的代码,但它需要大约20分钟,有更快的方法吗?

- (NSArray *)insertPricesqlWithPrice:(Price *) price{sqliteManager *dbInfo = [sqliteManager sharedsqliteManagerWithDataBasename:@"codefuel_catalogo.sqlite"];sqlite3 *database;Nsstring *querysql=[self formatStringqueryInsertWithtable:@"prices_List" andobject:price];if(sqlite3_open([dbInfo.dataBasePath UTF8String],&database) == sqlITE_OK){    sqlite3_stmt * compiledStatement;    const char *query_stmt = [querysql UTF8String];    int result = sqlite3_prepare_v2(database,query_stmt,-1,&compiledStatement,NulL);    if (result == sqlITE_OK)    {        int success = sqlite3_step(compiledStatement);        NSLog(@"el numero de success es -> %i",success);        if (success == sqlITE_ERROR)            NSLog(@"Error al insertar en la base de datps");    }    else        NSLog(@"Error %@ ERROR!!!!",querysql);    sqlite3_finalize(compiledStatement);}sqlite3_close(database);return nil;}
解决方法 为了加快插入速度,您需要做三件事:

>在循环外移动sqlite3_open的调用.目前,循环未显示,因此我假设它在您的代码段之外
>添加BEGIN TRANSACTION和COMMIT TRANSACTION调用 – 您需要在插入循环之前开始事务,并在循环结束后立即结束.
>使formatStringqueryInsertWithtable真正参数化 – 目前看来你没有充分利用预准备语句,因为尽管使用了sqlite3_prepare_v2,你的代码中没有调用sqlite3_bind_XYZ.

这是a nice post that shows you how to do all of the above.它是普通的C,但它可以作为Objective C程序的一部分正常工作.

char* errorMessage;sqlite3_exec(mDb,"BEGIN TRANSACTION",NulL,&errorMessage);char buffer[] = "INSERT INTO example VALUES (?1,?2,?3,?4,?5,?6,?7)";sqlite3_stmt* stmt;sqlite3_prepare_v2(mDb,buffer,strlen(buffer),&stmt,NulL);for (unsigned i = 0; i < mVal; i++) {    std::string ID = getID();    sqlite3_bind_text(stmt,1,ID.c_str(),ID.size(),sqlITE_STATIC);    sqlite3_bind_double(stmt,2,getDouble());    sqlite3_bind_double(stmt,3,4,getDouble());    sqlite3_bind_int(stmt,5,getInt());    sqlite3_bind_int(stmt,6,7,getInt());    if (sqlite3_step(stmt) != sqlITE_DONE) {        printf("Commit Failed!\n");    }    sqlite3_reset(stmt);}sqlite3_exec(mDb,"COMMIT TRANSACTION",&errorMessage);sqlite3_finalize(stmt);
总结

以上是内存溢出为你收集整理的ios – 如何将40000条记录快速插入iPad中的sqlite数据库全部内容,希望文章能够帮你解决ios – 如何将40000条记录快速插入iPad中的sqlite数据库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1102376.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存