Giter VIP home page Giter VIP logo

dbflowone's Introduction

DBFlow数据库配置及简单操作

操作流程

  1. 配置
  2. 初始化
  3. 建立数据库和表
  4. 增删改查

第一步:配置

1.1 在项目build.gradle添加apt插件和库

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

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

如图:

1.2 在app的build.gradle中配置

apply plugin: 'com.neenbedankt.android-apt'

def dbflow_version = "3.1.1"

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

如图:

第二步:初始化在项目中新建MyApp类,继承Application,并在Manifest中注册,在OnCrate方法中初始化DBFlow

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

如图:

第三步:建立数据库和表

3.1 新建AppDatabase类,配置表数据库名称和版本

@Database(name = AppDatabase.NAME, version = AppDatabase.VERSION)
public class AppDatabase {
//数据库名
public static final String NAME = "DBFlow_DB";
//数据库版本
public static final int VERSION = 1;
}

3.2 新建User表,继承实体基类BaseModel

@Table(database = AppDatabase.class)
public class User extends BaseModel {

//设置主键,并自动增长,从1开始
@PrimaryKey(autoincrement = true)
private long tableId;

@Column
private String name;

@Column
private int age;

public User() {
super();
}

public long getTableId() {
return tableId;
}

public void setTableId(long tableId) {
this.tableId = tableId;
}

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

第四步:增删改查

4.1 增加

User user = new User();
user.setName("张三");
user.setAge(18);
user.save();

4.2 删除

User user = new User();
user.setTableId(1);
user.delete();

4.3 更新

User user = new User();
user.setName("李四");
user.setTableId(1);
user.update();

4.4 查询

List<User> userList = SQLite.select().from(User.class).where(User_Table.name.eq("张三")).queryList();

注意:建立数据库和表后以一定要编译一次,不然不能执行表操作

dbflowone's People

Contributors

zhoucarson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.