Android数据库DbFlow

Android数据库框架DbFlow使用

打开项目的根目录下的build.gradle

如下:

Android数据库DbFlow_第1张图片

打开module下的build.gradle文件添加:

def dbFlow_version = "4.0.0-beta3"
在依赖下添加:

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}"

下面可以正式开始: 
创建application类以初始化DbFlow:


public class AppDb extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        FlowManager.init(new FlowConfig.Builder(this).build());
    }
}
不要忘记在清单文件下添加

android:name=".AppDb"
创建数据库类:

@Database(name = MyDatabase.NAME, version = MyDatabase.VERSION)
public class MyDatabase {
    static final String NAME = "DataBase";
    static final int VERSION = 1;
}
创建实体类: 
这里需要注意,修饰符必须大于default,要有自增id。

@Table(database = MyDatabase.class)
public class ContentBean extends BaseModel {
    @PrimaryKey(autoincrement = true)
    Long id;
    @Column
    String name;
    @Column
    String edg;
    @Column
    String sexy;
    @Column
    String gg;
    //省略Getter and  Setter。。。
}
     /**
     * 保存一条
     */
    private void saveSingle() {
        ContentBean mBean = new ContentBean();
        mBean.setEdg("18");
        mBean.setName("张三");
        mBean.setSexy("男");
        mBean.setGg("gg");
        mBean.save();
    }

    /**
     * 读取一条
     */
    private void readSingle() {
        ContentBean contentBeanList = SQLite.select().
                from(ContentBean.class).querySingle();
        if (contentBeanList != null)
            Log.d(TAG, "contentBeanList: " + contentBeanList.getEmail());
    }

    /**
     * 保存多条
     */
    private void saveList() {
        FlowManager.getDatabase(MyDatabase.class)
                .getTransactionManager()
                .getSaveQueue()
                .addAll2(SourceList.getList());
    }
//还需要执行下面操作    FlowManager.getDatabase(MyDatabase.class).getTransactionManager().getSaveQueue().purgeQueue();

    /**
     * 读取多条
     */
    private void readList() {
        List<ContentBean> contentBeanList = SQLite.select().
                from(ContentBean.class).queryList();
        Log.d(TAG, "contentBeanList.size  is:: " + contentBeanList.size());
        if (contentBeanList.size() > 1)
            Log.d(TAG, "contentBeanList: 1::" + contentBeanList.get(0).getName());
    }

你可能感兴趣的:(android)