Android 数据库 DBFlow 使用教程

github:
https://github.com/anymyna/ui

步骤:

1、 在项目下的build.gradle中添加

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://www.jitpack.io" }
    }
}

2、 在app下的bulid.gradle下添加

def dbFlow_version = "4.2.4"
dependencies {

    annotationProcessor "com.github.Raizlabs.DBFlow:dbflow-processor:${dbFlow_version}"
    implementation "com.github.Raizlabs.DBFlow:dbflow-core:${dbFlow_version}"
    implementation "com.github.Raizlabs.DBFlow:dbflow:${dbFlow_version}"
}

3、Application 中初始化

        FlowManager.init(new FlowConfig.Builder(this).build());

4、 创建数据库 AppDatabase

@Database(name = AppDatabase.NAME,version = AppDatabase.VERSION)
public class AppDatabase {
    public static final String NAME="AppDatabase";
    public static final int VERSION = 1;
}

5、 创建表 User2Model

public class User2Model extends BaseModel {

    @PrimaryKey(autoincrement = true)
    private int id;

    @Column
    private String name;

    @Column
    private int age;

    @Column
    private long timeStamp;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public long getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(long timeStamp) {
        this.timeStamp = timeStamp;
    }
}

6、 数据操作、插入数据、删除数据、更新数据、查询数据




       //插入数据  id 必须唯一
        User2Model userModel=new User2Model();
        userModel.setName("UserModel");
        userModel.setAge(1);
        userModel.insert();

        userModel=new User2Model();
        userModel.setName("UserModel");
        userModel.setAge(2);
        userModel.insert();

        userModel=new User2Model();
        userModel.setName("UserModel");
        userModel.setAge(3);
        userModel.insert();

        userModel=new User2Model();
        userModel.setName("UserModel");
        userModel.setAge(4);
        userModel.insert();

        userModel=new User2Model();
        userModel.setName("UserModel");
        userModel.setAge(5);
        userModel.insert();

        userModel=new User2Model();
        userModel.setName("UserModel");
        userModel.setAge(6);
        userModel.insert();

        SQLite.insert(User2Model.class)
                .columns(User2Model_Table.name,User2Model_Table.age)
                .values("angel",27)
                .execute();

        //删除数据
        userModel = SQLite.select()
                .from(User2Model.class)
                .where(User2Model_Table.id.is(3))
                .querySingle();
        if (userModel != null){
            userModel.delete();
        }

        SQLite.delete(User2Model.class)
                .where(User2Model_Table.name.is("UserModel"))
                .and(User2Model_Table.id.is(4))
                .async()
                .execute();

        //更新数据
                userModel = SQLite.select()
                .from(User2Model.class)
                .where(User2Model_Table.id.is(1))
                .querySingle();
        if (userModel != null){
            userModel.setName("update 1 ");
            userModel.update();
        }

        SQLite.update(User2Model.class)
                .set(User2Model_Table.name.eq("update 2"))
                .where(User2Model_Table.name.is("UserModel"))
                .and(User2Model_Table.id.is(5))
                .async()
                .execute();


        //查询全部
        List<User2Model> noteBeans = SQLite.select()
                .from(User2Model.class)
                .queryList();

        //条件查询
        userModel = SQLite.select()
                .from(User2Model.class)
                .where(User2Model_Table.name.eq("update 2"))
                .querySingle();

你可能感兴趣的:(Android)