import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.afa.tourism.greendao.gen.BeanDao;
import com.afa.tourism.greendao.gen.DaoMaster;
import com.afa.tourism.greendao.gen.DaoSession;
import com.example.zhoukao.bean.Bean;
import com.example.zhoukao.bean.User;
import org.greenrobot.greendao.query.QueryBuilder;
import java.util.List;
/**
public class MyUserDao {
Context context;public MyUserDao(Context context) {
this.context = context;
}
/**
* 插入一条记录
*
* @param user
*/
public void insertUser(Bean user) {
DaoMaster daoMaster = new DaoMaster(DaoMessage.getInstance(context).getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
BeanDao beanDao = daoSession.getBeanDao();
beanDao.insert(user);
}
public void deleteAll(){
DaoMaster daoMaster = new DaoMaster(DaoMessage.getInstance(context).getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
BeanDao beanDao = daoSession.getBeanDao();
beanDao.deleteAll();
}
/**
* 插入用户集合
*
* @param users
*/
public void insertUserList(List<Bean> users) {
if (users == null || users.isEmpty()) {
return;
}
DaoMaster daoMaster = new DaoMaster(DaoMessage.getInstance(context).getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
BeanDao beanDao = daoSession.getBeanDao();
beanDao.insertInTx(users);
}
/**
* 查询用户列表
*/
public List<Bean> queryUserList() {
DaoMaster daoMaster = new DaoMaster(DaoMessage.getInstance(context).getReadableDatabase());
DaoSession daoSession = daoMaster.newSession();
BeanDao beanDao= daoSession.getBeanDao();
QueryBuilder<Bean> qb = beanDao.queryBuilder();
List<Bean> list = qb.list();
return list;
}
/**
* 查询用户列表
*/
public void updateUser(Bean bean) {
DaoMaster daoMaster = new DaoMaster(DaoMessage.getInstance(context).getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
BeanDao beanDao = daoSession.getBeanDao();
beanDao.update(bean);
}
/**
* 删除一条记录
*
*
*/
public void deleteUser(Bean bean) {
DaoMaster daoMaster = new DaoMaster(DaoMessage.getInstance(context).getWritableDatabase());
DaoSession daoSession = daoMaster.newSession();
BeanDao beanDao = daoSession.getBeanDao();
beanDao.delete(bean);
}
}