/**
这段代码写的是查询数据库中的数据,并且是按照id排序(倒序),下列中是new一个Date对象(时间对象),然后根据时间的跨度进行查询的,下面new Date中有个坑
1.date对象,如果你不设置日期,那么就是当前的日子,所以你要用来查询跨度需要去设置 月份内的日子为 1 或者最后一天
2.date月份是从0开始的,我查的时候老是出错,然后慢慢看date发现,这个月份单词是什么,然后一查发现.从0开始的,好吧.程序员是0开始,但是我想的是,应该一月份是1才是正常人吧...
3.如果你查询当月,嗯,这个好想只能这么查,你用eq参数去查的时候,这个好像要完全相等,你肯定无法做到月份时分秒什么的完全相等吧.我想的话.
*/
public List<Personal> limitCategoryQuery(int startIndex) {
List<Personal> query = null;
try {
Dao<Personal, Integer> personalDataDao = openHelper.getPersonalDataDao();
QueryBuilder<Personal, Integer> queryBuilder = personalDataDao.queryBuilder();
PreparedQuery<Personal> prepare = queryBuilder.prepare();
Date date = new Date();
date.setMonth(0);
date.setYear(113);
Date date1 = new Date();
date.setYear(113);
date1.setMonth(1);
queryBuilder.orderBy("_id", false).limit(10).offset(startIndex).where().ge("rizi", date).and().lt("rizi", date1);
query = queryBuilder.query();
} catch (SQLException e) {
e.printStackTrace();
}
return query;
}
// 查询本地数据库的数据,分段查询
private List<Personal> queryDate() {
List<Personal> personals = RegisiterOpenHelper.getHelper(this).limitCategoryQuery(0);
List<Personal> querys = null;
try {
if (noMoreDate) {
Toast.makeText(QueryActivity.this, "没有更多数据", Toast.LENGTH_LONG).show();
return querys;
}
Dao<Personal, Integer> personalDataDao = RegisiterOpenHelper.getHelper(this).getPersonalDataDao();
QueryBuilder<Personal, Integer> queryBuilder = personalDataDao.queryBuilder();
// offset 就是说你从哪个数字开始, false表示倒序,true表示正序, limit是表示一次查多少个
// 毕竟手机内存有限,一次查几条,再显示才是王道.
queryBuilder.orderBy("_id", false).offset(startIndex).limit(LIMIT);
querys = queryBuilder.query();
startIndex += querys.size();
if (querys.size() < LIMIT) {
noMoreDate = true;
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return querys;
}
/** *数据库的存储表单,这个配置,可以直接生产响应的字段,其实还可以设置长度的,很多参数,可以看文档. */
@DatabaseTable(tableName = "regisger_table")
public class Personal {
public Personal() {
/*this.registerTime = new Date().getTime() +"";*/
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = simpleDateFormat.format(new Date());
this.registerTime = format;
this.rizi = new Date();
}
@DatabaseField(columnName = "rizi", dataType = DataType.DATE)
private Date rizi;
@DatabaseField(generatedId = true)
private int _id;
/** * 姓名 */
@DatabaseField(columnName = "name", canBeNull = true)
private String peopleName;
/** * 性别 */
@DatabaseField(columnName = "sex", canBeNull = true)
private String peopleSex;
/** * 民族 */
@DatabaseField(columnName = "nation", canBeNull = true)
private String peopleNation;
/** * 出生日期 */
@DatabaseField(columnName = "birthday", canBeNull = true)
private String peopleBirthday;
/** * 住址 */
@DatabaseField(columnName = "address", canBeNull = true)
private String peopleAddress;
/** * 身份证号 */
@DatabaseField(columnName = "idcardNum", canBeNull = true)
private String peopleIDCode;
/** * 签发机关 */
@DatabaseField(columnName = "department", canBeNull = true)
private String department;
/** * 有效期限:开始 */
@DatabaseField(columnName = "startDates", canBeNull = true)
private String startDate;
/** * 有效期限:结束 */
@DatabaseField(columnName = "endDate", canBeNull = true)
private String endDate;
/** *登记时间 */
@DatabaseField(columnName = "registerTime", canBeNull = true)
private String registerTime;
/** * 身份证头像 */
@DatabaseField(dataType = DataType.BYTE_ARRAY)
private byte[] photo;
/** * 没有解析成图片的数据大小一般为1024字节 */
private byte[] headImage;
/** * 三代证指纹模板数据,正常位1024,如果为null,说明为二代证,没有指纹模板数据 */
private byte[] model;
/** * 登记的事件 类型原因什么的 */
@DatabaseField(columnName = "workingReason", canBeNull = false)
private String workingReason;
public String getRegisterTime() {
return registerTime;
}
public String getWorkingReason() {
return workingReason;
}
public void setWorkingReason(String workingReason) {
this.workingReason = workingReason;
}
public void setRegisterTime(String registerTime) {
this.registerTime = registerTime;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getPeopleName() {
return peopleName;
}
public void setPeopleName(String peopleName) {
this.peopleName = peopleName;
}
public String getPeopleSex() {
return peopleSex;
}
public void setPeopleSex(String peopleSex) {
this.peopleSex = peopleSex;
}
public String getPeopleNation() {
return peopleNation;
}
public void setPeopleNation(String peopleNation) {
this.peopleNation = peopleNation;
}
public String getPeopleBirthday() {
return peopleBirthday;
}
public void setPeopleBirthday(String peopleBirthday) {
this.peopleBirthday = peopleBirthday;
}
public String getPeopleAddress() {
return peopleAddress;
}
public void setPeopleAddress(String peopleAddress) {
this.peopleAddress = peopleAddress;
}
public String getPeopleIDCode() {
return peopleIDCode;
}
public void setPeopleIDCode(String peopleIDCode) {
this.peopleIDCode = peopleIDCode;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
public byte[] getHeadImage() {
return headImage;
}
public void setHeadImage(byte[] headImage) {
this.headImage = headImage;
}
public byte[] getModel() {
return model;
}
public void setModel(byte[] model) {
this.model = model;
}
public Date getRizi() {
return rizi;
}
public void setRizi(Date rizi) {
this.rizi = rizi;
}
@Override
public String toString() {
return "Personal{" +
"_id=" + _id +
", peopleName='" + peopleName + '\'' +
", peopleSex='" + peopleSex + '\'' +
", peopleNation='" + peopleNation + '\'' +
", peopleBirthday='" + peopleBirthday + '\'' +
", peopleAddress='" + peopleAddress + '\'' +
", peopleIDCode='" + peopleIDCode + '\'' +
", department='" + department + '\'' +
", startDate='" + startDate + '\'' +
", endDate='" + endDate + '\'' +
", registerTime='" + registerTime + '\'' +
", photo=" + Arrays.toString(photo) +
", headImage=" + Arrays.toString(headImage) +
", model=" + Arrays.toString(model) +
", workingReason='" + workingReason + '\'' +
'}';
}
}
/** 这里创建只用了OpenHelper去继承框架类的方式,还有另外直接继承响应的Activity的方式实现,具体可以看文档. */
public class RegisiterOpenHelper extends OrmLiteSqliteOpenHelper {
private static RegisiterOpenHelper openHelper;
private static final String DATABASE_NAME = "registerinfo.db";
private static final int DATABASE_VERSION = 1;
private Dao<Personal, Integer> simpleDao = null;
private RuntimeExceptionDao<Personal, Integer> simpleRuntimeDao = null;
public RegisiterOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
try {
Log.i(RegisiterOpenHelper.class.getName(), "onCreate");
TableUtils.createTable(connectionSource, Personal.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {
}
/** * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached * value. */
public Dao<Personal, Integer> getPersonalDataDao() throws SQLException {
if (simpleDao == null) {
simpleDao = getDao(Personal.class);
}
return simpleDao;
}
/** * 单例 * @param cotext * @return */
public static RegisiterOpenHelper getHelper(Context cotext) {
if (openHelper == null) {
synchronized (RegisiterOpenHelper.class) {
if (openHelper == null) {
openHelper = OpenHelperManager.getHelper(cotext, RegisiterOpenHelper.class);
}
}
}
return openHelper;
}
/** * Close the database connections and clear any cached DAOs. */
@Override
public void close() {
super.close();
simpleDao = null;
}
}
总结:存储的时候,尽量选择相对应的格式,图方便都用String的话,无法进行大小的对比这种比较负责的查询操作.谨记!
下面的站点可能需要科学上网姿势.
github资源站点.android相关的
关于字段中的DateType相应的文档说明
ORMLTE的文档