gpt4 book ai didi

objective-c - 如何使用FMDB SDK 更改SQLite 表达式树的最大深度?

转载 作者:搜寻专家 更新时间:2023-10-30 20:09:16 24 4
gpt4 key购买 nike

我正在使用 FMDB 实现一个基于 SQLite 的应用程序,我需要向 SQLite 中插入大量数据,比如说 1000 行数据,但我不想一个一个地插入它们,我只想这样做使用单个查询,这会使插入进度更快,如下所示:

NSString *query = @"insert into table (c1,c2,c3,c4) values " ;
NSString *middle = @"" ;
for ( int i = 0 ; i < 1000 ; i++ ) {

NSDictionary *tmpItem = [data objectAtIndex:i] ;

if ( i == 999 ) {

middle = [NSString stringWithFormat:@"%@('%@','%@','%@','%@')", middle,[tmpItem objectForKey:@"c1"],[tmpItem objectForKey:@"c2"],[tmpItem objectForKey:@"c3"],[tmpItem objectForKey:@"c4"]] ;
}

else {

middle = [NSString stringWithFormat:@"%@('%@','%@','%@','%@') ,", middle,[tmpItem objectForKey:@"c1"],[tmpItem objectForKey:@"c2"],[tmpItem objectForKey:@"c3"],[tmpItem objectForKey:@"c4"]] ;
}
}

query = [NSString stringWithFormat:@"%@%@", query, middle] ;
[db executeUpdate:query];

上面的代码无法在“表”上插入任何数据,但是当我将for循环更改为只运行10次时,数据插入正确,这意味着没有语法错误。

我错误地尝试过,数据插入正确,for 循环设置为 i<500,而设置 i<501 时,没有插入数据。

然后我搜索http://www.sqlite.org/limits.html ,这个页面有一个Maximum Depth Of An Expression Tree,应该是我要设置的,但是在使用FMDB的时候怎么设置这个属性呢?

或者我可以插入大量数据的唯一方法就是将它们一条一条地插入?

最佳答案

您的选择并不局限于一次一个或全部。

您可以分批插入数据,这仍然会给您带来速度提升,但也更容易预测成功。更改限制仍然只是限制,仍然是限制(根据数据,您可能会遇到其他限制)。一次批处理 100 个似乎是合理的。

还要确保您没有在主 UI 线程上执行大量批处理工作。

除此之外,如果您编译自己的 sqlite 二进制文件,则您提供的 sqlite 链接具有预处理器编译设置。 FMDB 只是 sqlite 的包装器,默认情况下在 iOS 上您将获得与 iOS 打包的 sqlite。当然,您可以编译自己的并更改限制并链接到您的应用程序。

因此,您需要重新编译并链接限制为 0。请注意,运行时限制调用仅允许您降低编译限制,如果它设置为高于 0(默认为 100)

The maximum depth of an expression tree can be lowered at run-time using the sqlite3_limit(db,SQLITE_LIMIT_EXPR_DEPTH,size) interface if the SQLITE_MAX_EXPR_DEPTH is initially positive. In other words, the maximum expression depth can be lowered at run-time if there is already a compile-time limit on the expression depth. If SQLITE_MAX_EXPR_DEPTH is set to 0 at compile time (if the depth of expressions is unlimited) then the sqlite3_limit(db,SQLITE_LIMIT_EXPR_DEPTH,size) is a no-op.

关于objective-c - 如何使用FMDB SDK 更改SQLite 表达式树的最大深度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21134830/

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