several shell scripts writing to same sqlite db getting "database is locked" error.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
several shell scripts writing to same sqlite db getting "database is locked" error.
I'm getting a "SQL error: database is locked" when I have several shell scripts attempting to write to the same sqlite database. I understand why it's happening, but I wonder if there's a way I can prevent it from happening - is there a way to make sqlite wait until the file is no longer locked? I don't see it in the man page.
It should be known, I'm using the sqlite3 CLI from a shell script, so there are no API calls being made (I read on the sqlite website that there was an API option to cause it to wait if you're inserting from C).
You can set a timeout for this with the .timeout command before you do the SQL commands. For example:
.timeout 5000
select ...
Which means it should wait 5 seconds for a table to unlock before giving the error message.
Edit:
Your error message show the database is locked, not the table, so I'm not sure if it works. But I've written scripts where many processes access the same database, which work.
If the functionality isn't there, maybe your scripts could use a while loop to check for the existence of a 'flag' file, wait until the db is free (sleep), then create the 'flag' file once there's a successful open.
@ Guttorm - thanks for the link. I hadn't seen that doc before. That said, it doesn't appear to cover running commands from the shell. If it does, I don't see where to set any of the settings to which it refers (admittedly, I only scanned the article).
@ kbp - I did essentially that - I try to write to the db & then check the exit code after the write. If it's 0, I keep going, if it's non-zero, I wait 0.1 seconds & try again for up to X number of times before failing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.