从零开始的体温app开发(dao)
package dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import bean.User;
import db.Sqlite;
public class UserDao {
private final Sqlite sqlite;
private static UserDao userDao=null;
private UserDao(Context context){
sqlite =new Sqlite(context);
}
public static UserDao getInstance(Context context){
if(userDao==null){
userDao=new UserDao(context);
}
return userDao;
}
//保存
public void insert(User user){
SQLiteDatabase db=sqlite.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",user.get("name"));
values.put("num",user.get("num"));
values.put("cl",user.get("cl"));
values.put("date",user.get("date"));
values.put("time",user.get("time"));
values.put("area",user.get("area"));
values.put("tem",user.get("tem"));
values.put("qw",user.get("qw"));
db.insert("text",null,values);
db.close();
}
//获取
public List<User> findAll(){
SQLiteDatabase db=sqlite.getWritableDatabase();
Cursor cursor=db.query("text",new String[]{"name","num","cl","date","time","area","tem","qw"},null,null,null,null,null);
List<User> userList=new ArrayList<>();
while(cursor.moveToNext()){
User u=new User();
u.setName(cursor.getString(0));
u.setNum(cursor.getString(1));
u.setCl(cursor.getString(2));
u.setDate(cursor.getString(3));
u.setTime(cursor.getString(4));
u.setArea(cursor.getString(5));
u.setTem(cursor.getString(6));
u.setQw(cursor.getString(7));
userList.add(u);
}
db.close();
return userList;
}
}