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.
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.
I referred to the Official Document for both installation and usage. To install DBFlow, you need to add the following contents.
build.gradle
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"
}
To use DBFlow, you need to prepare a few new classes.
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>
AppDatabase.java
@Database(name = AppDatabase.NAME, version = AppDatabase.VERSION)
public class AppDatabase {
public static final String NAME = "AppDatabase";
public static final int VERSION = 1;
}
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;
}
--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();
@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