【问题标题】:SQLite. Can't add more than 1000 rowsSQLite。不能添加超过 1000 行
【发布时间】:2012-10-18 14:08:39
【问题描述】:

我正在尝试向我的 SQLite 数据库(使用 fmdb)添加 10k 行,但写入在 1000 行时停止。而且我没有任何错误或警告。我的代码:

NSString *queryString = [NSString stringWithFormat:@"insert into histories (storyid, text, date, storyurl) values (%li, ?, ?, ?)",history.storyIndex];
if ([self.db open]) {
    if([self.db executeUpdate:queryString, history.storyText, history.storyDate, history.storyUrl]) {
        NSLog(@"history added");
    } 
}

有什么建议吗?

【问题讨论】:

  • ...混合了 printf 格式和绑定变量?!?
  • @H2CO3 有很多小写 SQL 查询示例。对性能有不好的影响吗?
  • @RomanHouse 别担心,没关系。你只是没有足够的经验来理解妙语:)
  • @H2CO3 让我们不要拖延 :) 如果你给我一些使用大写查询的理由,我会很高兴 :)
  • @H2CO3 好的,我将使用大写查询并立即变得专业:D

标签: iphone objective-c ios sqlite fmdb


【解决方案1】:

对于遇到相同问题的任何人...确保您的 sqlite 查看器不会将查询的输出限制为 1000 个结果。

刚刚因为同样的问题浪费了几分钟,答案就这么简单!

【讨论】:

    【解决方案2】:

    你能不能试试下面的代码,看看有没有变化:

    NSString *queryString = @"INSERT INTO histories (storyid, text, date, storyurl) 
    VALUES ( ?, ?, ?, ?)";
    
    BOOL executeUpdateStatus = NO;
    
    if ([self.db open]) 
    {
        executeUpdateStatus = [self.db executeUpdate:queryString, 
        [NSNumber numberWithLong:history.storyIndex], history.storyText,
        history.storyDate, history.storyUrl];
    
    
            if(executeUpdateStatus == YES)   
                NSLog(@"history added");
            else
                NSLog(@"history NOT added");
    }
    

    【讨论】:

    • 你应该只打开你的数据库一次,不要忘记使用[self.db commit];
    【解决方案3】:

    数据库打开放在循环之外,这样它就只被调用一次。在 10,000 行的循环内重复调用它可能会导致内存问题。

    在每次执行更新后检查 db 对象上的 lastErrorCode,如果 lastErrorMessage 不为零,则 NSLog 记录它。

    通过增加一个计数器并使用模数算法来间隔提交,例如

    counter++;
    if (mod(counter,500) ){
        [self.db commit];
    }
    

    考虑使用 python 在 iOS 项目之外批量加载数据库。

    【讨论】:

      【解决方案4】:

      听起来您需要提交交易。您似乎已达到单个事务中的行数限制。

      【讨论】:

      • 我已经尝试提交每个事务,但没有帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      相关资源
      最近更新 更多