sqlite3 shell command '.backup' and transaction

大城市里の小女人 提交于 2019-12-12 10:44:20

问题


I have sqlite database on my local host (Win NT) and want to back it up. I want to use shell command '.backup' to accomplish this task. But it seems I can insert a new rows into database during backup process.

Does '.backup' shell command starts new exclusive transaction on my database?

I thought when I execute '.backup' shell command it locks my database.


回答1:


The sqlite3 backup method does not lock the database. I would suggest to use the following workaround if you would like to lock the database:

  • Start a transaction (shared lock)
  • By using any INSERT statement, the database gets a reserved lock. However, this INSERT statement can be empty.
  • Backup the database.
  • End the transaction by using a ROLLBACK or COMMIT.

Code:

BEGIN;
INSERT INTO <anytable> SELECT * FROM <anytable> WHERE 1=0;
.backup <database> <file>
ROLLBACK;

A less hacky way would be if you are using a table named 'backup' and you are inserting a row (date,..) for each copy (if this information is relevant for you).



来源:https://stackoverflow.com/questions/10895776/sqlite3-shell-command-backup-and-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!