LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2010, 07:33 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
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).
 
Old 07-08-2010, 01:49 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

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.

I found this page with info about it:
http://www.sqlite.org/lockingv3.html

Last edited by Guttorm; 07-08-2010 at 02:00 AM.
 
Old 07-08-2010, 01:50 AM   #3
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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.
 
Old 07-08-2010, 10:28 PM   #4
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
@ 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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python / sqlite - database locked despite large timeouts acid_kewpie Programming 3 04-08-2010 05:44 AM
why it`s saying "urmpi database locked"? manas Linux - Software 18 05-21-2007 04:28 PM
run adapt but "package system database is locked" ctdarksilver Ubuntu 4 08-24-2006 04:27 AM
Why do I always get "urpmi database locked?" Ausar Linux - Newbie 2 08-11-2004 12:09 PM
Writing shell script called "convert" bluewolf Programming 3 02-04-2003 10:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration