DBFlow数据库的简单使用(增删改查)

第一步:

在项目下的build.gradle中添加

maven{url"https://jitpack.io"}

第二步:

在app下的bulid.gradle下添加:

annotationProcessor "com.github.Raizlabs.DBFlow:dbflow-processor:4.1.2"
// gradle 3.0.0 可以使用 implementation,否则用 compile
implementation "com.github.Raizlabs.DBFlow:dbflow-core:4.1.2"
implementation "com.github.Raizlabs.DBFlow:dbflow:4.1.2"

第三步:

创建数据库:Database

@Database(version = DataBase.VERSION)
public class DataBase {
    public static final int VERSION=1;
}

创建表:Product

@Table(database = DataBase.class)
public class Product extends BaseModel{
    @PrimaryKey(autoincrement = true)
    public long id;
    @Column
    public String name;
    @Column
    public long price;
}

第四步:

增删改查:CurdUtils

package com.imageswitchview.ych.dbflow.Utils;
import com.imageswitchview.ych.dbflow.Table.Category;
import com.imageswitchview.ych.dbflow.Table.Product;
import com.imageswitchview.ych.dbflow.Table.Product_Table;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import java.util.List;
public class CrudUtils {
    //添加
    public static void insert() {
        //方法一
        Product product = new Product();
        product.name = "yy";
        product.save();
        //对没有继承BaseModel的实体
        //方法一
        Product product1 = new Product();
        product1.name="yy";
        FlowManager.getModelAdapter(Product.class).insert(product1);

        //方法二
        SQLite.insert(Product.class)
                .columnValues(Product_Table.name.eq("yy"))
                .execute();
    }

    //删除
    public static void delete() {
        //方法一  先查后删除
        Product product = SQLite.select()
                .from(Product.class)
                .where(Product_Table.name.eq("yy"))
                .querySingle();
        if (product!=null){
            product.delete();
        }
        //方法二 直接删除
       SQLite.delete(Product.class)
               .where(Product_Table.name.eq("yy"))
               .execute();
    }

    //修改
    public static void update() {
        //方法一 先查后改
        Product product = SQLite.select()
                .from(Product.class)
                .where(Product_Table.name.eq("yy"))
                .querySingle();//区别于queryList(),返回的是实体
        if (product != null) {
            product.name = "yy1";
            product.update();
        }

        //方法二 直接修改
        SQLite.update(Product.class)
                .set(Product_Table.name.eq("yy1"))
                .where(Product_Table.name.eq("yy"))
                .execute();
    }

    //查询全部
    public static List<Product> selectAll() {
        //方法一
        List<Product> products = SQLite.select()
                .from(Product.class)
                .where()
                // .orderBy(Product_Table.id,true)//按照升序
                // .limit(5)//限制条数
                .queryList();//返回的list不为空,但是可能为empty
        return products;
    }

    //查询单个
    public static Product selectOne() {
        Product product = SQLite.select()
                .from(Product.class)
                .where(Product_Table.name.eq("yy"))//条件
                .querySingle();//返回单个实体
        return product;
    }
}

第五步:

入口:MainActivity

package com.imageswitchview.ych.dbflow;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.raizlabs.android.dbflow.config.FlowManager;
import static com.imageswitchview.ych.dbflow.Utils.CrudUtils.insert;
import static com.imageswitchview.ych.dbflow.Utils.CrudUtils.selectAll;
import static com.imageswitchview.ych.dbflow.Utils.CrudUtils.selectOne;
import static com.imageswitchview.ych.dbflow.Utils.CrudUtils.update;
import static com.raizlabs.android.dbflow.sql.language.SQLite.delete;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FlowManager.init(this);//应用启动时,首先初始化
        //执行增删改查
        insert();
//        delete();
//        update();
//        selectOne();
//        selectAll();
    }
}

demo简单,请见谅!

猜你喜欢

转载自www.cnblogs.com/2484ydv/p/9257800.html