欢迎使用CSDN-markdown编辑器

1.配置环境
project build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath ‘com.android.tools.build:gradle:2.3.2’
classpath ‘com.jakewharton:butterknife-gradle-plugin:8.5.1’
classpath ‘org.greenrobot:greendao-gradle-plugin:3.2.2’ // add plugin
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

app build.gradle 
      apply plugin: 'org.greenrobot.greendao'

           compile 'org.greenrobot:greendao:3.2.2'

2.新建bean文件 自动生成 dao和session
设置自动生成目录
app build.gradle
greendao {
schemaVersion 3
daoPackage “com.bcinfo.htjk.dao”
targetGenDir ‘src/main/java/’
}

3.注册数据库
application
private void setupDatabase() {
helper = new DaoMaster.DevOpenHelper(this, “greendao.db”, null);
db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}

public DaoSession getDaoSession() {
    return daoSession;
}

public SQLiteDatabase getDb() {
    return db;
}

4.增删改查
语句 可以使用 sql原生的查询语句 也可以使用queryBuilder

查询语句 注意 的 查询条件是表中的字段名
// 根据user_id查询本地数据
Query query = applyPartsInfoDao.queryBuilder().where(new WhereCondition.StringCondition(“USER_ID = ” + PreferenceUtils.getInt(getActivity(), “user_id”))).build();
applylist.clear();
if (query.list().size() > 0) {
applylist.addAll(query.list());
}
}
删除
/**
* 提交成功后,删除本地数据
*/
private void deleteLocalData(int id) {
String sql = “delete from ” + ApplyPartsInfoDao.TABLENAME + ” where ” + ApplyPartsInfoDao.Properties.DspId.columnName
+ “=” + id + “” + ” and ” + ApplyPartsInfoDao.Properties.UserId.columnName
+ “=” + PreferenceUtils.getInt(getActivity(), “user_id”);
applyPartsInfoDao.getDatabase().execSQL(sql);
}

增加
insert可以增加单个对象 也可以是list 插入 集合速度很快

update 方法 传值对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值