Simple use of Dbflow database (add and revise)

Source: Internet
Author: User
Tags sqlite



The first step:



Add in Build.gradle under Project


Maven{url "Https://jitpack.io"}





Step Two:



Under Bulid.gradle in the app, add:





annotationProcessor "com.github.Raizlabs.DBFlow:dbflow-processor:4.1.2"
// gradle 3.0.0 can use implementation,or use compile
implementation "com.github.Raizlabs.DBFlow:dbflow-core:4.1.2"
implementation "com.github.Raizlabs.DBFlow:dbflow:4.1.2"





Step Three:



Database creation:





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


CREATE TABLE: Product





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


Fourth Step:



Delete and change: 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 {
/ / add
public static void insert() {
/ / method 1
Product product = new Product();
product.name = "yy";
product.save();
//For entities that do not inherit basemodel
/ / method 1
Product product1 = new Product();
product1.name="yy";
FlowManager.getModelAdapter(Product.class).insert(product1);
/ / method two
SQLite.insert(Product.class)
.columnValues(Product_Table.name.eq("yy"))
.execute ();
}
/ / delete
public static void delete() {
//Method 1 check before delete
Product product = SQLite.select()
.from(Product.class)
.where(Product_Table.name.eq("yy"))
.querySingle();
if (product!=null){
product.delete();
}
//Method 2 delete directly
SQLite.delete(Product.class)
.where(Product_Table.name.eq("yy"))
.execute ();
}
/ / modification
public static void update() {
//Method 1 check first, then change
Product product = SQLite.select()
.from(Product.class)
.where(Product_Table.name.eq("yy"))
. querysingle(); / / different from querylist(), it returns entity
if (product != null) {
product.name = "yy1";
product.update();
}
//Method 2 direct modification
SQLite.update(Product.class)
.set(Product_Table.name.eq("yy1"))
.where(Product_Table.name.eq("yy"))
.execute ();
}
//Query all
public static List<Product> selectAll() {
/ / method 1
List<Product> products = SQLite.select()
.from(Product.class)
.where ()
//. orderby (product_table. ID, true) / / in ascending order
//. limit (5) / / limit the number of entries
. querylist(); / / the returned list is not empty, but may be empty
return products;
}
//Query single
public static Product selectOne() {
Product product = SQLite.select()
.from(Product.class)
. where (product_table. Name. EQ ("YY") / / condition
. querysingle(); / / returns a single entity
return product;
}
}


Fifth Step:



Entrance: 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); / / when the application is started, initialize it first
//Add, delete, modify and check
Insert ();
//        delete();
//        update();
//        selectOne();
//        selectAll();
}
} 





Demo simple, please forgive me!






Simple use of Dbflow database (add and revise)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.