您的位置:首页 > 数据库

xutil数据库整理

2015-09-21 15:34 260 查看
1、JSONObject.toJSON(list)将list数组用josn打印出来

2、xUutil框架中更新数据库数据,知道一条数据查找数据库中一条记录

String date=2015-12-01;
PeriodBaen  baen=null;
for(PeriodBaen  p:  periodBaenArrayList){
if(date.equals(p.getData())){
baen=p;
break;
}
}
baen.setInterval_num(30);
PeriodDao.getInstance().UpDate(baen);


3、创建数据库已经其中的方法

public class PeriodBaen implements Serializable {
private int id;
private String data;
private int interval_num;
private int period_num;
private String menstruation;
private String easy_pregnancy;
private String ovulation;
private String forecast;
private int Month = 0;

public void setId(int id) {
this.id = id;
}

public void setData(String data) {
this.data = data;
}

public void setInterval_num(int interval_num) {
this.interval_num = interval_num;
}
.....
....
...

@Override
public String toString() {
return "PeriodBaen{" +
"id=" + id +
", data='" + data + '\'' +
", interval_num=" + interval_num +
", period_num=" + period_num +
", menstruation='" + menstruation + '\'' +
", easy_pregnancy='" + easy_pregnancy + '\'' +
", ovulation='" + ovulation + '\'' +
", forecast='" + forecast + '\'' +
", Month=" + Month +
'}';
}
}


public class PeriodDao {

private PeriodDao() {
}

private static PeriodDao periodDao = null;

public static PeriodDao getInstance() {
if (periodDao == null) {
periodDao = new PeriodDao();
}
return periodDao;
}

public void savePeriodDao(PeriodBaen periodBaen) throws DbException {
ArrayList<PeriodBaen> periodBaenArrayList = (ArrayList) this.getPeriodDao();
//        ELog.e("======"+ JSONObject.toJSON(periodBaenArrayList));
if (periodBaenArrayList != null && periodBaenArrayList.size() != 0) {
for (int i = 0; i < periodBaenArrayList.size(); i++) {
if (periodBaen.getData().equals(periodBaenArrayList.get(i).getData())) {
if(periodBaen.getPeriod_num()!=periodBaenArrayList.get(i).getPeriod_num()){
this.deletePeriodDao(periodBaenArrayList.get(i));
periodBaenArrayList.get(i).setPeriod_num(periodBaen.getPeriod_num());
}
return;
}
}

}
new DaoConfig(HawaApp.context).getDbUtils().save(periodBaen);
}

public List<PeriodBaen> getPeriodDao() throws DbException {
//        order by id desc limit 0,5;
return new DaoConfig(HawaApp.context).getDbUtils().findAll(com.lidroid.xutils.db.sqlite.Selector.from(PeriodBaen.class).orderBy("data", false).limit(6));
//        return (ArrayList<PeriodBaen>) new DaoConfig(HawaApp.context).getDbUtils().findAll(PeriodBaen.class);
}

public PeriodBaen findByMonth(int Month) throws DbException {
return new DaoConfig(HawaApp.context).getDbUtils().findFirst(com.lidroid.xutils.db.sqlite.Selector.from(PeriodBaen.class).where("Month", "=", Month));
}

public void deletePeriodDao(PeriodBaen periodBaen) throws DbException {
new DaoConfig(HawaApp.context).getDbUtils().delete(periodBaen);
}

public void daleteAll(){
try {
new DaoConfig(HawaApp.context).getDbUtils().deleteAll(PeriodBaen.class);
} catch (DbException e) {
e.printStackTrace();
}
}

public void daleteDate(String str){
try {
new DaoConfig(HawaApp.context).getDbUtils().delete(PeriodBaen.class,WhereBuilder.b("data","=",str));
} catch (DbException e) {
e.printStackTrace();
}
}

/**
* 修改
* @param baen
*/
public void UpDate(PeriodBaen  baen){
try {
new DaoConfig(HawaApp.context).getDbUtils().update(baen);
} catch (Exception e) {
e.printStackTrace();
}
}
}


4、修改的具体步骤–自己慢慢看暂不整理

```
//update interval_num=11 period where id=?? or date="2012-02-02";
//                DbUtils db___=DbUtils.create(this);
//                List<PeriodBaen> list5=db___.findAll(PeriodBaen.class);
//                ArrayList<PeriodBaen> list2 = (ArrayList) this.getPeriodDao();
//                ELog.i(">]xyl list1="+ JSONObject.toJSON(list));
String date=""+MoreSelectDate.get(MoreSelectDate.size()-1);
PeriodBaen  baen=null;
for(PeriodBaen  p:  periodBaenArrayList){
if(date.equals(p.getData())){
baen=p;
break;
}
}
//                ELog.i(">]xyl list2="+JSONObject.toJSON(periodBaenArrayList));
//                ELog.i(">]xyl date="+date);
//                ELog.i(">]xyl bean="+JSONObject.toJSON(baen));
////                DbUtils db=DbUtils.create(this);
////                baen=db.findById(PeriodBaen.class,baen.getId());
//                baen=new DaoConfig(HawaApp.context).getDbUtils().findById(PeriodBaen.class,baen.getId());
//                ELog.i(">]xyl bean2="+JSONObject.toJSON(baen));
//                baen.setInterval_num(DateUtil.daysBetween(MoreSelectDate.get(MoreSelectDate.size()-1),selectDate));
//                new DaoConfig(HawaApp.context).getDbUtils().update(baen);
//                ELog.i(">]xyl bean3="+JSONObject.toJSON(baen));
//                new DaoConfig(HawaApp.context).getDbUtils();
baen.setInterval_num(DateUtil.daysBetween(MoreSelectDate.get(MoreSelectDate.size()-1),selectDate));
PeriodDao.getInstance().UpDate(baen);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: