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"/>

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.


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


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())

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.

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