在第一阶段开发时用到的dao层方法,对sqlite数据库的增删改查
1.注册
public boolean register(User user){ SQLiteDatabase sdb=dbHelper.getReadableDatabase(); String sql="insert into user_login(username,password,age,sex,uid) values(?,?,?,?,?)"; Object obj[]={user.getUsername(),user.getPassword(),user.getAge(),user.getSex(),user.getUid()}; sdb.execSQL(sql, obj); return true; }
2.注册
public boolean login(String username){ SQLiteDatabase sdb=dbHelper.getReadableDatabase(); String sql="select * from user_login where username=?"; Cursor cursor=sdb.rawQuery(sql, new String[]{username}); if(cursor.moveToFirst()==true){ cursor.close(); return true; } return false; }
3.添加信息
public boolean insert(Bean bean){//添加用户信息的方法 SQLiteDatabase db = dbHelper.getReadableDatabase(); Date date0 = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); String date = simpleDateFormat.format(date0); ContentValues values = new ContentValues(); values.put("username",bean.getUsername()); values.put("date",date); values.put("userid",bean.getUserid()); values.put("heat",bean.getHeat()); values.put("place",bean.getPlace()); values.put("health",bean.getHealth()); values.put("condition",bean.getCondition()); long insert = db.insert("infomax",null,values); //System.out.println(temBean.getPlace()); return insert>0; }
4.查询信息
public List<Bean> queryAll(){ List<Bean> list=new ArrayList<>(); Bean bean=null; SQLiteDatabase db=dbHelper.getReadableDatabase(); Cursor c=db.query("infomax",null,null,null,null,null,null,null); if(c!=null&&c.getCount()>0){ list=new ArrayList<>(); while(c.moveToNext()){ bean=new Bean(); bean.setUsername(c.getString(c.getColumnIndex("username"))); bean.setDate(c.getString(c.getColumnIndex("date"))); bean.setUserid(c.getString(c.getColumnIndex("userid"))); bean.setPlace(c.getString(c.getColumnIndex("place"))); bean.setHeat(c.getString(c.getColumnIndex("heat"))); bean.setCondition(c.getString(c.getColumnIndex("condition"))); bean.setId(c.getInt(c.getColumnIndex("id"))); list.add(bean); } } return list; }