欢迎使用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 方法 传值对象

原创粉丝点击