[JAVA] Summary of using DBFlow

Introduction

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.

build.gradle


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

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>.

MyApplication.java


public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        FlowManager.init(this);
    }
}

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

AndroidManifest.xml



 <application
        android:name=".MyApplication" />
 </application>

Database definition

AppDatabase.java


@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>.

User.java


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

    @Column
    @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;
        user.insert();

--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()
                .from(User.class)
                .where(Condition.column(User$Table.NAME).is(name))
                .queryList();

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()
                .from(User.class)
                .where(
                    Condition.column(User$Table.NAME).is(name),
                    Condition.column(User$Table.AGE).is(age)
                 )
                .queryList();

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

    @Override
    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>(
                act,
                android.R.layout.simple_list_item_1
        );

        List<User> list = new Select()
                .from(User.class)
                .where(Condition.column(User$Table.USER_NAME).is(name))
                .queryList();

        for (int i = 0; i < list.size(); i++) {
            adapter.add(String.valueOf(list.get(i).age));
        }

        listView.setAdapter(adapter);

        return view;
    }

Recommended Posts