Skip to content
Andrew Horishnii edited this page Dec 16, 2015 · 2 revisions

Creating database

###There are two ways to create a database on the device:

1. Copy database from assets folder.

  • put the complete database to the assets folder.
  • in you db helper class that extends from LeftDBUtils need call setDBContext(Context context, String name, int version) with real file name. For example:
setDBContext(context, "sample.sqlite", 1);

####2. Create a database using SQL queries. In this way in you db helper class that extends from LeftDBUtils need call setDBContext(Context context, String name, int version) with not existing file name.

Also in you db helper class that extends from LeftDBUtils need override onCreate(SQLiteDatabase db). This is where the creation of tables and the initial population of the tables should happen. You can use createTable(SQLiteDatabase db, Class> type) or createTables(SQLiteDatabase db, List> elements) for automatic creating database tables from classes. For example:

@Override
public void onCreate(SQLiteDatabase db) {
    super.onCreate(db);
    createTable(db, SimpleEntity.class);
}

For automatic creating relationship use: createRelationship(@NonNull SQLiteDatabase db, @NonNull Class> type) - for existing tables; createTable(@NonNull SQLiteDatabase db, @NonNull Class> type, @Nullable RelationshipConfig relationship), createTable(@NonNull SQLiteDatabase db, @NonNull Class<?> type, @Nullable Collection relationships) - for new tables;

Clone this wiki locally