[JAVA] Summary of using DBFlow


Since I used a fast (?) Library called DBFlow, I will summarize how to install and use it. The contents will be updated ** from time to time **.

The development environment is as follows.

What is DBFlow

ORM (Object-relational mapping) library provided by Raizlabs. ** DBFlow is a library that can simplify the processing around the database such as table definition by using annotations ( @ ~ </ code>) **.

It seems to be famous for being fast in the streets.

  • Regarding the processing speed, Raizlabs' Comparison result is summarized in the link. Compared with DBFlow, GreenDAO, OrmLite, Ollie, Realm, the result that DBFlow is faster is introduced.

Introduction of DBFlow

I referred to the Official Document for both installation and usage. To install DBFlow, you need to add the following contents.

  • build.gradle(Project)
    Add the following contents in dependencies.


dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7'
  • build.gradle(app)


dependencies {
    apt 'com.raizlabs.android:DBFlow-Compiler:2.2.1'
    compile "com.raizlabs.android:DBFlow-Core:2.2.1"
    compile "com.raizlabs.android:DBFlow:2.2.1"

How to use DBFlow

To use DBFlow, you need to prepare a few new classes.

  1. (Create new if not available) Class that initializes DBFlow (MyAppilcation.java)
  2. Class for declaring database (AppDatabase.java)
  3. Class for declaring table (User.java)

Initialize DBFlow

Initialize with FlowManager.init (this) </ code>.


public class MyApplication extends Application {

    public void onCreate() {

If you create a new one, add the following description to the application tag of AndroidManifest.xml.


        android:name=".MyApplication" />

Database definition


@Database(name = AppDatabase.NAME, version = AppDatabase.VERSION)
public class AppDatabase {

    public static final String NAME = "AppDatabase";

    public static final int VERSION = 1;


Table definition

The column must be @Column </ code> and the primary key must be @PrimaryKey </ code>.


@Table(databaseName = AppDatabase.NAME, tableName = "USER_TABLE")
public class User extends BaseModel { 

    @PrimaryKey(autoincrement = true)
    int id;

    @Column(name = "NAME")
    public String name;

    @Column(name = "AGE")
    public int age;

  • Of course, you can also add private. In that case, don't forget to create a setter and getter.

How to write a query

--Insert statement

        User user = new User();
        user.name = "takenoki";
        user.age = 20;

--Select statement

select NAME from USER_TABLE where NAME = "takenoki";

The above SQL statement is converted for DBFlow as follows.

     List<User> list = new Select()

In DBFlow, clauses used in SQL are prepared as methods, and in addition to the above, distinct (), count (), etc. can also be used. This time, we are getting the data with the same name, but if you want to get the data with the same name and age, it will be as follows.

     List<User> list = new Select()

Code that sets the query execution result in Listview and displays it

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sample, container, false);

        String name = "takenoki";
        int age = 20;

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(

        List<User> list = new Select()

        for (int i = 0; i < list.size(); i++) {


        return view;

Recommended Posts