1. 程式人生 > >DBFlow數據庫的簡單使用(增刪改查)

DBFlow數據庫的簡單使用(增刪改查)

date() prim 可能 save his ces utils pre roc

第一步:

在項目下的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簡單,請見諒!

DBFlow數據庫的簡單使用(增刪改查)