On Mon, Dec 15, 2008 at 1:43 PM, P Kishor <punk.k...@gmail.com> wrote:

> On 12/15/08, Joanne Pham <joannekp...@yahoo.com> wrote:
> > Hi All,
> >  I have this problem about open the database. Here is the detail about
> the problem.
> >
> >  Our application have one process to create the database and another
> process to open the database and creating the report.
> >  The problem here is the database is not created but if the second
> process has tried to access the database then the empty database is created
> which has the size of 0. So the question is there any way the open database
> API should return an error message instead of creating the empty database
> when the second process opens the database.
> >
> >  Thanks,
>
>
> A SQLite database is just a file on the hard disk. Test for the
> existence of the file before trying to open it.
>

That is, unfortunately, a recipe for race conditions.  Application A tests
for whether the file exists and finds it doesn't.  The operating system task
switches to Application B which had previously determined that the file
doesn't exist, and now creates it and adds some data.  Task switch back to
Application A which assumes that the file doesn't exist (since it had
previously determined that) and scribbles over the data just written by
Application B.

An alternative is to open the database normally (sqlite3_open) and issue the
queries

  BEGIN EXCLUSIVE;
  SELECT 1 FROM sqlite_master LIMIT 1;

If you get back a value (1), then the database had already has tables (or
triggers or ...) in it.  If you get back NULL then you can go ahead and
create the database tables et al.  When done creating tables and doing
whatever else you need to do before allowing other applications access,
issue a COMMIT.  Since you had an exclusive transaction, any other
application trying to determine if the database is available or not will
block until your transaction completes.

Derrell
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to