Is SQLite thread-safe?

  • Short answer: Yes
  • Medium length answer:
    1.Be sure to recompile with -DTHREADSAFE=1
2.Do not use the same database connection at the same time in more than one thread.

3.On some operating systems, a database connection should always be used in the same thread in which it was originally created.
4.There are a few features of SQLite that are not threadsafe. Avoid those features.

  • Longer answer:

By “threadsafe” we mean that you can use different SQLite database connections in different threads at the same time.

线程安全:并发情况下,在不同的线程中操作不同的数据库句柄是安全的。

It has never been safe to use the same database connection simultaneously in multiple threads.

If you use the sqlite3_prepare() API to create prepared statements, each prepared statement is considered to be a part of the database connection from which it was derived. So you cannot run two prepared statements originating from the same database connection in different threads at the same time.

Conclusion

  • Make sure you’re compiling SQLite with -DTHREADSAFE=1.
  • Make sure that each thread opens the database file and keeps its own sqlite structure.
  • Make sure you handle the likely possibility that one or more threads collide when they access the db file at the same time: handle SQLITE_BUSY appropriately.
  • Make sure you enclose within transactions the commands that modify the database file, like INSERT, UPDATE, DELETE, and others.

转载地址:
http://www.sqlite.org/cvstrac/wiki?p=MultiThreading

    原文作者:Crazy2015
    原文地址: https://www.jianshu.com/p/fc63426099bf
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞