SQLite3 and multiple processes

前端 未结 4 1923
广开言路
广开言路 2020-12-08 15:18

How should one ensure correctness when multiple processes access one single SQLite database file?

4条回答
  •  庸人自扰
    2020-12-08 15:46

    Basically you need to wrap your data access code with transactions. This will keep your data consistent. Nothing else is required.

    In SQLite you are using

    BEGIN TRANSACTION

    COMMIT TRANSACTION

    pairs to delimit your transactions. Put your SQL code in between in order to have it execute in a single transaction.

    However, as previous people have commented before me - you need to pay close attention for concurrency issues. SQLite can work reasonably fast if it used for read access (multiple readers are not blocked and can run concurrently).

    However - the picture changes considerably if your code interleaves write and read access. With SQLite - your entire database file will be locked if even a single writer is active.

提交回复
热议问题