[odb-users] Creating schema and adding tables without dropping data

Ella Vishnevsky ellav at checkpoint.com
Wed Apr 20 03:52:17 EDT 2016


I'm using 2.3.0 version & sqlite
I wanted to implement the following scenario:

1.       Check if sqlite db file exists:

a.       Does not exist :

                                                               i.      Create new file with all tables that have the "db object" pragma.

b.      Exists:

                                                               i.      Add the new tables without erasing data from old tables

I started with the first one, but seems that the file is not being created.
This is the field :      odb::database  *m_db;
Create function:     void Init_OneDB(const std::string &_db_name, long _mode = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
Implementation:   m_db = new odb::sqlite::database("TEST_ONE_DB",_mode);

Although the file does not exist, an empty file is not being created.

a.       I thought of using schema_catalog::exists(*m_db) but it returns true.

b.      I thought of persisting dummy entity but I get crash :  terminate called after throwing an instance of 'odb::sqlite::database_exception'

  what():  1 (0): no such table: person.

c.       I looked at the example of schema/embedded and creating it by using query on person & creating schema if exception is being caught.
That worked but how can it be implemented when a file exists, but new tables should be inserted.
For example : my sqlite file contains person table. If the sqlite file does not exit - > the query fails -> I reach the catch exception and use the schema_catalog::create_schema.
Now I added a new table : person2. How can I "merge" it? The code will query person->see that it exits -> and won't use create_schema -> person2 will not be added.
On the other hand, if I always use create_schema - I will delete all my data.

       m_db = new odb::sqlite::database("TEST_ONE_DB",_mode);

       //if (odb::schema_catalog::exists(*m_db))


       typedef odb::query<CPerson> query;
       typedef odb::result<CPerson> result;

       odb::transaction t (m_db->begin ());

              m_db->query<CPerson> (false);
       catch(const odb::exception& e)
              std::cout << "***** CREATING SCHEMA  ****** " << std::endl;
              schema_catalog::create_schema (*m_db);


More information about the odb-users mailing list