Are Database operations thread safe?

2019-08-20 06:47发布

问题:

I am using sqlite in my iPhone app.

I have some database operations, in which I have to insert into two tables different data(means there is no data-dependency). Can I perform these two operations in seperate thread. While the insert operation in each table are more than one. So I am doing this in a while loop also.

回答1:

SQLite itself can be built to be thread-safe (for v3.3.1 or later, if it is compiled with SQLITE_THREADSAFE defined). You can verify this by calling sqlite_threadsafe().

I suggest you read the following carefully:

  • SQLite FAQ on threading

Even if SQLite is thread-safe, you may wish to revisit your design. Having multiple threads write to the database is probably not ideal, and it may be possible to redesign to avoid the requirement altogether.