换汤不换药之家庭记账本app开发(dao)
package dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
import bean.user;
import sqlite.db;
public class userdao {
public double eat=0;
public double play=0;
public double clothes=0;
public double live=0;
public double usual=0;
public double total=0;
private final db Db;
private static userdao userDao=null;
private userdao(Context context){
Db =new db(context);
}
public static userdao getInstance(Context context){
if(userDao==null){
userDao=new userdao(context);
}
return userDao;
}
public db getDb() {
return Db;
}
//保存
public void insert(user u){
SQLiteDatabase db=Db.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("date",u.get("date"));
values.put("eat",u.get("eat"));
values.put("play",u.get("play"));
values.put("clothes",u.get("clothes"));
values.put("live",u.get("live"));
values.put("usual",u.get("usual"));
db.insert("tex",null,values);
db.close();
}
//获取
public void delete(String id) {
SQLiteDatabase db =Db.getWritableDatabase();
if (db.isOpen()) {
db.execSQL("delete from tex where date=?", new Object[]{id});
db.close();
}
}//删除
public List<user> findAll(){
SQLiteDatabase db=Db.getWritableDatabase();
Cursor cursor=db.query("tex",new String[]{"date","eat","play","clothes","live","usual"},null,null,null,null,null);
List<user> userList=new ArrayList<>();
while(cursor.moveToNext()){
user u=new user();
u.setDate(cursor.getString(0));
u.setEat(cursor.getString(1));
u.setPlay(cursor.getString(2));
u.setClothes(cursor.getString(3));
u.setLive(cursor.getString(4));
u.setUsual(cursor.getString(5));
//Log.e("sqliteshow",user.getName()+" "+user.getDate()+" "+user.getTime()+" "+user.getTem()+" "+user.getArea());
userList.add(u);
}
total=eat+play+clothes+live+usual;
db.close();
return userList;
}
public void Initdata(){
SQLiteDatabase db=Db.getWritableDatabase();
Cursor cursor=db.query("tex",new String[]{"date","eat","play","clothes","live","usual"},null,null,null,null,null);
while(cursor.moveToNext()){
eat = eat + Double.valueOf(cursor.getString(1));
play = play + Double.valueOf(cursor.getString(2));
clothes = clothes + Double.valueOf(cursor.getString(3));
live = live + Double.valueOf(cursor.getString(4));
usual = usual + Double.valueOf(cursor.getString(5));
}
total=eat+play+clothes+live+usual;
db.close();
}
public void clear(){
eat=0;play=0;clothes=0;live=0;usual=0;total=0;
}
}