从零开始的体温app开发(dao2)
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 bean.User2;
import db.Sqlite;
import db.Sqlite2;
public class UserDao2 {
private final Sqlite2 sqlite;
private static UserDao2 userDao=null;
private UserDao2(Context context){
sqlite =new Sqlite2(context);
}
public static UserDao2 getInstance(Context context){
if(userDao==null){
userDao=new UserDao2(context);
}
return userDao;
}
//保存
public void insert(User2 user){
SQLiteDatabase db=sqlite.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("num",user.getNum());
values.put("name",user.getName());
values.put("cl",user.getCl());
values.put("phone",user.getPhone());
db.insert("tex2",null,values);
db.close();
}
//获取
public boolean pkong(){
boolean temp=false;
SQLiteDatabase db=sqlite.getWritableDatabase();
Cursor cursor=db.rawQuery("select *from tex2",null);
if(cursor.getCount()!=0){
temp=true;
}
return temp;
}
public List<User2> findAll(){
SQLiteDatabase db=sqlite.getWritableDatabase();
Cursor cursor=db.query("tex2",new String[]{"num","name","cl","phone"},null,null,null,null,null);
List<User2> userList=new ArrayList<>();
while(cursor.moveToNext()){
User2 user2=new User2();
user2.setNum(cursor.getString(0));
user2.setName(cursor.getString(1));
user2.setCl(cursor.getString(2));
user2.setPhone(cursor.getString(3));
//Log.e("sqliteshow",user.getName()+" "+user.getDate()+" "+user.getTime()+" "+user.getTem()+" "+user.getArea());
userList.add(user2);
}
db.close();
return userList;
}
}