ios - 尝试插入 10000 条记录 sqlite ios swift

标签 ios swift sqlite sqlite.swift

我正在尝试插入 10000 条记录,这需要 45 秒

这是我的代码

println(NSDate.new())
for index in 0...10000{
countrys.insert(name <- "abc")
//println(index)
}
println(NSDate.new())

是这样吗?

最佳答案

问题是 SQLite 必须单独提交每个 INSERT 语句。您应该考虑使用事务。您可以使用 transaction 方法(执行 BEGIN TRANSACTION SQL)启动事务,然后使用 commit 提交它们(执行 COMMIT SQL)。


例如:

db.transaction(.Deferred) { txn in
    for i in 0 ... 10000 {
        if countries.insert(name <- "abc").statement.failed {
            return .Rollback
        }
    }

    return .Commit
}

关于ios - 尝试插入 10000 条记录 sqlite ios swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31107680/

相关文章:

java - 从 SQLite 从游标读取数据时发生致命异常

iOS/Swift : Keep "global" view pan from resetting other objects' positions, w/AutoLayout ON?

ios - AVAudioPlayer 无法在共享类中快速工作

android - 有没有办法为应用程序( native 或混合)创建指纹扫描仪?

ios - 在扩展中添加和删除 Notification Observer

ios - 使用 Alamofire 序列化 JSON 对象

sql - Windows 上的 SQLite 数据库是否有等效的 MySQLAdmin 或 SQL Server Management Studio?

python - SQLITE3 将 WHERE 语句误认为列名

ios - Sprite Kit 中玩家移动的最佳路径

ios - 我可以通过 UISearchController 以编程方式复制 UIStatusBar 上的点击吗?