gpt4 book ai didi

iOS - 提高 SQLite 的插入性能

转载 作者:行者123 更新时间:2023-12-02 06:08:48 25 4
gpt4 key购买 nike

如何提高在 SQLite 数据库中插入数据的性能。

数据量为30000条记录。代码是:

- (void)insertBranchDB:branchlistArray
{
const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
int count = 0;
for (Branch *branch in branchlistArray) {
NSString *insertSQL = [NSString stringWithFormat:
@"insert or replace into Branch (id, lang, lat, lng, province_id, district_id, optional, bus, status, name, address, contact, telephone, remark, province_name, district_name, lastupdate) values (\"%@\", \"%@\", \"%@\", \"%@\",\"%@\", \"%@\", \"%@\", \"%@\",\"%@\", \"%@\", \"%@\", \"%@\",\"%@\", \"%@\", \"%@\", \"%@\", \"%@\")",branch._id,branch.lang,branch.lat,branch.lng,branch.province_id,branch.district_id,branch.optional,branch.bus,branch.status,branch.name,branch.address,branch.contact,branch.telephone,branch.remark,branch.province_name,branch.district_name,branch.lastupdate];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE) {
NSLog(@"Completed to Add Branch %d",count);
count;
}
else {
NSLog(@"Failed to Add Branch %d",count);
count;
}
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
}

最佳答案

如果您使用事务,您将看到性能的显着提高。因此,在执行所有 INSERT 语句之前,请执行 BEGIN TRANSACTION,最后执行 COMMIT(或 COMMIT TRANSACTION 结束交易)。

请参阅Transactions讨论http://sqlite.org .

关于iOS - 提高 SQLite 的插入性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34410521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com