Trying to insert 10000 records sqlite ios swift

1.6k views Asked by At

I am trying to insert 10000 records and its taking 45 secs

this is my code

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

is this way to do it?

1

There are 1 answers

5
Rob On BEST ANSWER

The issue is that SQLite will have to commit each of the INSERT statements individually. You should consider using transactions. You can start a transaction with transaction method (which performs BEGIN TRANSACTION SQL) and then use commit to commit them (which performs COMMIT SQL).


For example:

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

    return .Commit
}