2015-12-14 12:40 GMT+01:00 Clemens Ladisch <clemens at ladisch.de>:

> Cecil Westerhof wrote:
> > I have a crontab job which uses a SQLite database. Sometimes this
> database
> > is locked because I used SQLite DB Browser, but did not Write or Revert
> > Changes. It looks like that when a database is locked there is a file
> with
> > the database name with -journal appended to it. Can I count on this?
>
> The -journal file exists when there is some changed data that might
> need to be rolled back.  It is possible for the DB to be locked before
> some changed data is actually written.
>
> In WAL mode, there is no journal.  (And if your job is read only, it
> then would not be blocked by concurrent writes.)
>

?No, I need to write also.?




> > Then I could write a script to warn me about the lock.
>
> You could use SQLite to check whether the DB is locked:
>
>   if ! sqlite3 my.db "begin immediate"; then
>     echo "is locked"
>   fi
>

?Thanks, I improved a little on it:
sqlite3 "${DATABASE}" "begin immediate" 2>/dev/null
errorCode="${?}"
if [[ "${errorCode}" -eq 5 ]] ; then
    printf "${DATABASE} is locked\n"
elif [[ "${errorCode}" -ne 0 ]] ; then
    printf "Error ${errorCode} while accessing ${DATABASE}\n"
else
    printf "${DATABASE} is free\n"
fi

I saw that when it is locked I get back a 5. Is this always the case? In
this way I can see the difference between a lock and another error.

?If this code is correct I rewrite it for crontab. ;-)?

-- 
Cecil Westerhof

Reply via email to