Android开发---DBFlow数据库使用
个人学习记录以及方法,不喜勿喷!!!1、在model APP中添加如下:def dbflow_version = "4.1.2"dependencies {/*** 数据库* */annotationProcessor "com.github.Raizlabs.DBFlow:dbflow-processor:${dbflow_version...
·
个人学习记录以及方法,不喜勿喷!!!
1、在model APP中添加如下:
def dbflow_version = "4.1.2" 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}" }
2、在项目级的model中添加如下:
buildscript { repositories { google() jcenter() mavenCentral() // add repository } dependencies { classpath 'com.android.tools.build:gradle:3.4.2' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() mavenCentral() maven { url "https://maven.google.com" } maven { url "https://jitpack.io" } } }
3、初始化
FlowManager.init(new FlowConfig.Builder(this).build());
4、开始使用
@Table(database = AppDatabase.class) public class MarketBean extends BaseModel implements Serializable { @PrimaryKey(autoincrement = true)//ID自增 public long id; //合约编码 @Column private String instrument_id ="0"; //合约代码 @Column private String instrument_code; @Column private String iinstrument_code;
写上get、set。。。。
}
5、编写相关数据库的操作代码方法(根据自己的需要写,我就贴上我自己需要用到的):
public class XSqlUtils { //查询全部 public static List<MarketBean> selectAll() { //方法一 List<MarketBean> products = SQLite.select() .from(MarketBean.class) .where() .orderBy(MarketBean_Table.id, true)//按照升序 // .limit(5)//限制条数 .queryList();//返回的list不为空,但是可能为empty return products; } //查询单个 public static MarketBean selectOne(String instrument_id) { MarketBean product = SQLite.select() .from(MarketBean.class) .where(MarketBean_Table.instrument_id.eq(instrument_id))//条件 .querySingle();//返回单个实体 return product; } //添加 public static void insert(String instrument_id, String instrument_code, String iinstrument_code) { //方法一 MarketBean product = new MarketBean(); product.setInstrument_id(instrument_id); product.setInstrument_code(instrument_code); product.setIinstrument_code(iinstrument_code); product.save(); } //先查询后删除 public static void delete(String instrument_id) { //方法一 先查后删除 MarketBean product = SQLite.select() .from(MarketBean.class) .where(MarketBean_Table.instrument_id.eq(instrument_id)) .querySingle(); if (product != null) { product.delete(); } } //直接删除 public static void deleteOne(String instrument_id) { //方法二 直接删除 SQLite.delete(MarketBean.class) .where(MarketBean_Table.instrument_id.eq(instrument_id)) .execute(); } }
6、可以调用这些方法进行数据库的操作了。
完结!
更多推荐
已为社区贡献1条内容
所有评论(0)