Search code examples
androidandroid-sqlitedbflow

Can you use the DBFlow Android ORM with an existng Database Packaged with the app?


Can you use the DbFlow ORM with an existing SQLite Database. That is packaged with the app and copied (from assets folder) over on initial install?


Solution

  • Add your db foo.db in the assets folder and set your database class FooDatabase as the follow:

    @Database(name = FooDatabase.NAME, version = FooDatabase.VERSION)
    public class FooDatabase {
    
        public static final String NAME = "foo";
        public static final int VERSION = 1;
    
    }
    

    note the name is the same without the .db.

    Now suppose you have a table FooTable with a column id and a column fooName, we needs to represent that table as the follow:

    import com.raizlabs.android.dbflow.structure.BaseModel;
    
    @Table(databaseName = FooDatabase.NAME)
    public class FooTable extends BaseModel {
    
        @Column @PrimaryKey(autoincrement = true) long id;
        @Column String fooName;
    
    }
    

    it is all, test it, add some values to db and log it to show:

    List foo = new Select().from(FooTable.class).queryList()