//先过去GreenDao的对象 mGreenBeanDao
private DaoSession mDaoSession;
private GreenBeanDao mGreenBeanDao;
mDaoSession = MyApplication.getInstance().getDaoSession();
mGreenBeanDao = mDaoSession.getGreenBeanDao();
mGreenBeanDao.loadAll(); //查询所有
精确查找,若有多个或没有直接崩溃 查询的是当前ID 查询单个
GreenBean unique = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Id.eq("1").unique();
精确查找,查询多个
List<GreenBean> list = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Id.eq("1").list();
精确查找 Id > 输入的值 // where():相当于 &&
List<GreenBean> list = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Id.gt(mEt_query.getText().toString())).list();
/精确查找 Id < 输入的值
List<GreenBean> list = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Id.lt(mEt_query.getText().toString())).list();
多条件查询 查询neme是否为输入的值 id十位为3
mGreenBeanDao .queryBuilder().whereOr(GreenBeanDao.Properties.Name.eq(mEt_query.getText().toString()),
GreenBeanDao.Properties.Id.eq(3)).list();
unique和list的区别:
详细细节请观看源码:
package org.greenrobot.greendao;
import java.util.Collection;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.query.WhereCondition;
import org.greenrobot.greendao.query.WhereCondition.PropertyCondition;
/**
* Meta data describing a property mapped to a database column; used to create WhereCondition object used by the query builder.
*
* @author Markus
*/
public class Property {
public final int ordinal;
public final Class<?> type;
public final String name;
public final boolean primaryKey;
public final String columnName;
public Property(int ordinal, Class<?> type, String name, boolean primaryKey, String columnName) {
this.ordinal = ordinal;
this.type = type;
this.name = name;
this.primaryKey = primaryKey;
this.columnName = columnName;
}
/** Creates an "equal ('=')" condition for this property. */
public WhereCondition eq(Object value) {
return new PropertyCondition(this, "=?", value);
}
/** Creates an "not equal ('<>')" condition for this property. */
public WhereCondition notEq(Object value) {
return new PropertyCondition(this, "<>?", value);
}
/** Creates an "LIKE" condition for this property. */
public WhereCondition like(String value) {
return new PropertyCondition(this, " LIKE ?", value);
}
/** Creates an "BETWEEN ... AND ..." condition for this property. */
public WhereCondition between(Object value1, Object value2) {
Object[] values = { value1, value2 };
return new PropertyCondition(this, " BETWEEN ? AND ?", values);
}
/** Creates an "IN (..., ..., ...)" condition for this property. */
public WhereCondition in(Object... inValues) {
StringBuilder condition = new StringBuilder(" IN (");
SqlUtils.appendPlaceholders(condition, inValues.length).append(')');
return new PropertyCondition(this, condition.toString(), inValues);
}
/** Creates an "IN (..., ..., ...)" condition for this property. */
public WhereCondition in(Collection<?> inValues) {
return in(inValues.toArray());
}
/** Creates an "NOT IN (..., ..., ...)" condition for this property. */
public WhereCondition notIn(Object... notInValues) {
StringBuilder condition = new StringBuilder(" NOT IN (");
SqlUtils.appendPlaceholders(condition, notInValues.length).append(')');
return new PropertyCondition(this, condition.toString(), notInValues);
}
/** Creates an "NOT IN (..., ..., ...)" condition for this property. */
public WhereCondition notIn(Collection<?> notInValues) {
return notIn(notInValues.toArray());
}
/** Creates an "greater than ('>')" condition for this property. */
public WhereCondition gt(Object value) {
return new PropertyCondition(this, ">?", value);
}
/** Creates an "less than ('<')" condition for this property. */
public WhereCondition lt(Object value) {
return new PropertyCondition(this, "", value);
}
/** Creates an "greater or equal ('>=')" condition for this property. */
public WhereCondition ge(Object value) {
return new PropertyCondition(this, ">=?", value);
}
/** Creates an "less or equal ('<=')" condition for this property. */
public WhereCondition le(Object value) {
return new PropertyCondition(this, "<=?", value);
}
/** Creates an "IS NULL" condition for this property. */
public WhereCondition isNull() {
return new PropertyCondition(this, " IS NULL");
}
/** Creates an "IS NOT NULL" condition for this property. */
public WhereCondition isNotNull() {
return new PropertyCondition(this, " IS NOT NULL");
}
}
//模糊查询
//注意:得先插入小明
// List list = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Name.like("小明%")).list();
// List list = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Name.like("%小明%")).list();
List<GreenBean> list = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Name.like("%小明")).list();
//按照Key(键) id删除
mGreenBeanDao.deleteByKey(Long.valueOf(mEtDelete.getText().toString().trim()));
// mGreenBeanDao .delete(); //删除一条// mGreenBeanDao .deleteInTx(); //删除多个对象
// Long[] keys = {5L,6L};
// mGreenBeanDao .deleteByKeyInTx(keys); //以Key(id)删除多个对象
// mGreenBeanDao .deleteAll(); //删除所有数据
//注意:name不能重复,因为引用了 @Unique 注解
mGreenBeanDao.update(new GreenBean(Long.valueOf(mEtUpDate.getText().toString().trim()), "已修改", "已修改" + index++, index++));
// mGreenBeanDao.updateInTx(List); //修改多个 ,通过Key修改
mDaoSession.clear();// 清理缓存: MyApplication.getInstance().getDaoSession();
mGreenBeanDao.detachAll();// 清理所有缓存:
如何在手机中查看数据库生成文件:
外部存储:
通过Context.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据
通过Context.getExternalCacheDir()方法可以获取到 SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据