Let's have an Airport
Airport has many Airplanes
Each Airplane has many Flights (Each Flight belongs to Airplane)
Each Flight has many Passengers (Each Passenger belongs to Flight)
Each Passenger belongs to Service /first or second class/ (so Service has many Passengers)
And now let's play with all this data and Commit and save it, or Rollback at once!
Add src/com/vangelov/sqlite/AVModel.java to your project.
Create AirplaneModel.java
public class AirplaneModel extends AVModel {
public AirplaneModel() {}
public AirplaneModel(SQLiteDatabase db) { super(db); }
@Override
public String getTableName() { return "AIRPLANES"; }
@Override
public String getPrimaryKey() { return "ID"; }
@Override
public void setRelations() {
setHasMany("flights", FlightModel.class, "AIRPLANE_ID");
}
}
Create FlightModel.java
public class FlightModel extends AVModel {
public FlightModel() {}
public FlightModel(SQLiteDatabase db) { super(db); }
@Override
public String getTableName() { return "FLIGHTS"; }
@Override
public String getPrimaryKey() { return "ID"; }
@Override
public void setRelations() {
setBelongsTo("airplane", AirplaneModel.class, "AIRPLANE_ID");
setHasMany("passengers", PassengerModel.class, "FLIGHT_ID");
}
}
Create PassengerModel.java
public class PassengerModel extends AVModel {
public PassengerModel() {}
public PassengerModel(SQLiteDatabase db) { super(db); }
@Override
public String getTableName() { return "PASSENGERS"; }
@Override
public String getPrimaryKey() { return "ID"; }
@Override
public void setRelations() {
setBelongsTo("flight", FlightModel.class, "FLIGHT_ID");
setBelongsTo("service", ServiceModel.class, "SERVICE_ID");
}
}
Create ServiceModel.java
public class ServiceModel extends AVModel {
public ServiceModel() {}
public ServiceModel(SQLiteDatabase db) { super(db); }
@Override
public String getTableName() { return "SERVICES"; }
@Override
public String getPrimaryKey() { return "ID"; }
@Override
public void setRelations() { }
}
private AirplaneModel airplaneModel;
private SQLCollection airplanes;
airplaneModel = new AirplaneModel((SQLiteDatabase)mDb);
airplanes = airplaneModel.find_all();
private SQLRow selectedPlane = null;
selectedPlane = (SQLRow) airplanes.get(position);
SQLCollection flights = (SQLCollection) selectedPlane.getRelation("flights");
SQLRow newFlight = (new FlightModel(mAirportDatabase.mDb)).newRow();
flights.add(newFlight);
SQLRow airplane = (SQLRow) newFlight.getRelation("airplane");
airplane.put("is_busy", 0);
airplanes.save();
public SQLRow newRow();
public int update(ContentValues values,String whereClause,String[] whereArgs);
public long replace(ContentValues initialValues);
public long replaceOrThrow(ContentValues initialValues);
public long insert(ContentValues values);
public long insertOrThrow(ContentValues values);
public int delete(String whereClause,String[] whereArgs);
public SQLRow find(int id);
public SQLCollection find_with_limit(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);
public SQLCollection find_distinct(boolean distinct, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);
public SQLCollection find_with_factory(SQLiteDatabase.CursorFactory cursorFactory,boolean distinct, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);
public SQLCollection find_all();
public SQLCollection find_all(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy);
public SQLRow find_one(String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy);
public Object getRelation(String key,boolean reload);
public int remove();
public int save();
public void save();
public void clear(boolean includeRelatedCollections);
public boolean remove(Object object,boolean includeRelatedCollections);