欢迎使用CSDN-markdown编辑器
来源:互联网 发布:淘宝助理怎么使用5.5 编辑:程序博客网 时间:2024/05/16 12:50
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 方法 传值对象
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- Unity与VS关联
- 高中逍遥游快速记忆
- 微信分享实践和踩坑实践
- [绍棠_Swift] Swift3.0中Get、Post的同步与异步请求
- 设计模式-12 代理模式
- 欢迎使用CSDN-markdown编辑器
- 面向对象 练习题4
- POJ 2349 Arctic Network
- android 控件背景设置渐变色
- 用智能音箱+AI 联网玩狼人杀将是怎样的体验?
- 电脑重启后nagios报错Unable to get process status
- C++类与对象
- ue4 fatal error LNK1181 Engine\Binaries\Win64\UE4-UMGEditor.lib
- 移动端去除横向滚动条