DBFlow 的优势
select(name,screenSize).from(Android.class).where(name.is("Nexus 5x")).and(version.is(6.0)).querySingle()
-
Open Source(开源):DBFlow 时开源的,开源地址:Github
-
Robust(健壮性):支持 Trigger,ModelView,Index,Migration 以及内置的管理数据库的方式,此外,还支持 SQLCipher,RXJava 等
-
Multiple Databases,Multiple Modules(多数据库、多模型):无缝支持多数据库文件以及使用 DBFlow 的其他依赖中的数据库模型
-
Built On SQLite(基于 SQLite):SQLite 是世界上使用最广泛的数据库引擎,它不仅限于某个平台。
配置 DBFlow
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
def dbFlow_version = "4.2.4"
dependencies {
annotationProcessor "com.github.Raizlabs.DBFlow:dbflow-processor:${dbFlow_version}"
compile "com.github.Raizlabs.DBFlow:dbflow-core:${dbFlow_version}"
compile "com.github.Raizlabs.DBFlow:dbflow:${dbFlow_version}"
}
java.lang.NoSuchMethodError: com.raizlabs.android.dbflow.annotation.Table.tableName()Ljava/lang/String
/**
* 自定义Application
* @author jzman
* create at 2018/4/16 0016 17:28
*/
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
FlowManager.init(new FlowConfig.Builder(this).build());
FlowLog.setMinimumLoggingLevel(FlowLog.Level.V);
}
}
<application
android:name=".app.MyApplication"
</application>
创建数据库
/**
* MyDatabase
* @author jzman
* create at 2018/4/17 0017 9:08
*/
@Database(name = MyDatabase.NAME,version = MyDatabase.VERSION)
public class MyDatabase {
public static final String NAME = "MyDatabase";
public static final int VERSION = 1;
}
创建表
/**
* NoteTable.java
* @author jzman
* create at 2018/4/17 0017 9:54
*/
@Table(database = MyDatabase.class)
public class NoteTable extends BaseModel {
@Column
@PrimaryKey
int id;
@Column
private String title;
@Column
private String date;
@Column
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
java.lang.IllegalArgumentException: expected type but was null
插入数据
/**
* 插入数据
* @param model
*/
public void inseartData(NoteBean model){
model.setTitle("title");
model.setDate("2018-04-17");
model.setContent("content");
model.insert();
SQLite.insert(NoteBean.class)
.columns(NoteBean_Table.title,NoteBean_Table.date,NoteBean_Table.content)
.values("title","2018-04-17","content")
.execute();
}
删除数据
/**
* 删除数据
* @param model
*/
public void deleteData(NoteBean model){
model.delete();
SQLite.delete(NoteBean.class)
.where(NoteBean_Table.title.is("title"))
.and(NoteBean_Table.id.is(10))
.async()
.execute();
Delete.table(NoteBean.class);
Delete.table(NoteBean.class,NoteBean1.class);
}
更新数据
/**
* 更新数据
* @param model
*/
public void updateData(NoteBean model) {
model.update();
SQLite.update(NoteBean.class)
.set(NoteBean_Table.title.eq("title"),NoteBean_Table.content.eq("content"))
.where(NoteBean_Table.id.is(10))
.async()
.execute();
}
查询数据
/**
* 查询数据
*/
public List<NoteBean> queryData(){
List<NoteBean> noteBeans = SQLite.select()
.from(NoteBean.class)
.where(NoteBean_Table.title.is("title"))
.queryList();
return noteBeans;
}
案例