Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from a locked SQLite3 database

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 way 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.

like image 848
Pierre Bernard Avatar asked Feb 20 '10 10:02

Pierre Bernard


People also ask

How do you fix SQLite database is locked?

You should replace the backup database with the primary database. After the successful completion of the queries, the backup file will replace the locked database. As the backup file does not have any lock, it will be ready for the new transactions and queries by the user.

How do I access data in sqlite3?

First, establish a connection to the SQLite database by creating a Connection object. Next, create a Cursor object using the cursor method of the Connection object. Then, execute a SELECT statement. After that, call the fetchall() method of the cursor object to fetch the data.

Can you access SQLite remotely?

SQLite can still be made to work in many remote database situations, but a client/server solution will usually work better in that scenario.


1 Answers

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.

like image 107
Nate Avatar answered Sep 20 '22 19:09

Nate