开发者

Reading from a locked SQLite3 database

开发者 https://www.devze.com 2022-12-20 15:05 出处:网络
I am looking at a SQLite database owned by a third party process. It appears locked and has a *-journal file. What I don\'t know, is if the lock is shared or exclusive.

I am looking at a SQLite database owned by a third party process. It appears locked and has a *-journal file. What I don't know, is if the lock is shared or exclusive.

I am hoping to read from the database even though it is currently locked by that other process. I will only ever read from the database.

Currently I fail at this. I get a SQLITE_BUSY return code as long as the third party process is running.

I have looked at sqlite3_busy_handler, but that does not appear to be the solution. From what I understand that only allows for implementing a retry mechanism. It doesn't seem to offer a wa开发者_运维百科y to just ignore the fact, that the database is locked.

How can I force SQLite into reading from that database?

BTW, I am currently using the FMDatabase API wrapper. This does not use sqlite3_busy_handler. It loops endlessly as long as it gets a SQLITE_BUSY return code.


I've been doing some research on this and it looks like there are some (very undesirable) options.

  • Kill the process that has the database locked

This probably isn't an option for you and it isn't for me either.

  • Copy the file and read the copy

This seems to be the best solution to this problem. In my case I'm trying to read a Firefox sqlite database. Firefox appears to lock the file for long periods of time so I can't just wait.

Because you are copying a live database file you could get a corrupted copy. I don't think there is risk of corrupting the original (but I'm not sure about this).

Simply copy the file on the shell like

copy original.sqlite copy.sqlite

or

cp  original.sqlite copy.sqlite

Then open as normal.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号