LiteSQL
Creating and Dropping Database

The code below defines a simple database:

<?xml version="1.0"?>
<!DOCTYPE database SYSTEM "litesql.dtd">
<database name="PersonDatabase">
<object name="Person">
<field name="name" type="string"/>
</object>
</database>

The database file is created when PersonDatabase is constructed. First argument of constructor is requested backend. Other possibilities are "postgresql" and "mysql". Refer to class documentation for details.

PersonDatabase db("sqlite3", "database=person.db");

The database has to be created before any Persistent - classes can be used.

db.create();

The database can be dropped using drop() - method.

db.drop();

Suppose the database schema has changed along with new features of the program. Few data fields has been added to Person-class and completely new Persistent-class House has been defined. To upgrade database on disk, use needsUpgrade() and upgrade() - methods.

if (db.needsUpgrade())
db.upgrade();

If new fields has been added to classes, the upgrade operation may be quite costly. All backends do not support table modifications so records has to be copied forth and back.

Upgrade routine will try to preserve data. It will drop fields that are not in new schema and insert NULL-values to new fields.

litesql::Database::needsUpgrade
bool needsUpgrade() const
returns true if schema of backend database is different from schema returned by getSchema.
Definition: database.cpp:205
litesql::Database::upgrade
void upgrade() const
tries to upgrade database on disk so that schemas would be compatible
Definition: database.cpp:219
litesql::Database::drop
void drop() const
converts CREATE-statements (of getSchema) to DROP statements and executes them
Definition: database.cpp:188
litesql::Database::create
void create() const
creates database based on getSchema
Definition: database.cpp:179

SourceForge.net Logo