Storing and Deleting Persistents

A simple Person database:

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

Following code demonstrates how to insert Persons to database:

PersonDatabase db("sqlite3", "database=person.db"); // assumes the database has been created

Person person(db); // construct Person, does not write anything to database

person.name = "Bob"; // assign values to fields
person.age = 20;
person.update(); // writes a new record to database

person.age = 21; // Bob got just older
person.update(); // updates old record

person.id = 100; // force internal identifier (id) to 100 
person.update(); // updates old record
Note: if internal identifier (id-field) is changed, relations will not "follow" the object and will not be deleted either. If the object is not replaced with another object, relations should be manually dropped using delRelations-method.

Following code demonstrates how to delete Persons from database:

Person person = select<Person>(db).one(); // any Person will do

person.del(); // person.onDelete() gets called before data is deleted

SourceForge.net Logo