[odb-users] schema_catalog::exists

Boris Kolpackov boris at codesynthesis.com
Mon Oct 21 02:50:24 EDT 2013


Hi,

dd <durga.disc at gmail.com> writes:

>  I tried with below line to find out where test.db exists or not?
> 
>  bool b = odb::schema_catalog::exists(odb::id_sqlite, "test.db");

No, this is not going to work. schema_catalog::exists() checks
whether a schema exists in the application, not whether a
database file exists on disk. In fact, I am very surprised you
got this impression after reading the documentation. Perhaps
you *did not* read the documentation?


> What is the correct way to find the database existence. If not, I
> have to create the test.db.

The recommended way is to pass the SQLITE_OPEN_CREATE flag to the
odb::sqlite::database constructor. This will create an empty
database file if one does not exist. Then you will need detect
the empty database case and create the schema.

In the upcoming version of ODB there will be a proper way of
detecting this as part of the schema evolution support. For now
you can run a native query like this:

if (db.execute ("SELECT 1 FROM sqlite_master "
                "WHERE type='table' AND name='<table_name>'") == 0)
{
  schema_catalog::create_schema (db);
}

Where <table_name> is a name of a table corresponding to one of
your persistent classes.

Boris



More information about the odb-users mailing list