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.


SourceForge.net Logo