SQlite数据库查找功能
代码如下:
Dao.java(其中Constants.TABLE_NAME为数据库的表名)
1 public class Dao { 2 3 private final MyDatabaseHelper mHelper; 4 5 public Dao(Context context){ 6 //创建数据库 7 mHelper=new MyDatabaseHelper(context); 8 } 9 10 public List<persion> Query(String name){ 11 SQLiteDatabase db=mHelper.getWritableDatabase(); 12 List<persion> list=new ArrayList<persion>(); 13 String sql="select * from "+Constants.TABLE_NAME+" where name=?"; 14 Cursor cursor=db.rawQuery(sql,new String[]{name}); 15 while (cursor.moveToNext())// 判断Cursor中是否有数据 name,data,time,local,temperature 16 { 17 // 如果有用户,则把查到的值填充这个用户实体 18 String persion_Name=cursor.getString(cursor.getColumnIndex("name")); 19 String persion_Date=cursor.getString(cursor.getColumnIndex("data")); 20 String persion_Time=cursor.getString(cursor.getColumnIndex("time")); 21 String persion_Local=cursor.getString(cursor.getColumnIndex("local")); 22 String persion_Temperature=cursor.getString(cursor.getColumnIndex("temperature")); 23 String persion_Instruction=cursor.getString(cursor.getColumnIndex("instruction")); 24 persion per = new persion(persion_Name,persion_Date,persion_Time,persion_Local,persion_Temperature,persion_Instruction); 25 list.add(per); 26 } 27 return list;// 返回一个用户给前台 28 } 29 30 }
persion.java
1 public class persion { 2 String name; 3 String date; 4 String time; 5 String local; 6 String temperature; 7 String instruction; 8 9 public String getInstruction() { 10 return instruction; 11 } 12 13 public void setInstruction(String instruction) { 14 this.instruction = instruction; 15 } 16 17 public String getTemperature() { 18 return temperature; 19 } 20 21 public void setTemperature(String temperature) { 22 this.temperature = temperature; 23 } 24 25 public String getLocal() { 26 return local; 27 } 28 29 public void setLocal(String local) { 30 this.local = local; 31 } 32 33 public String getTime() { 34 return time; 35 } 36 37 public void setTime(String time) { 38 this.time = time; 39 } 40 41 public String getDate() { 42 return date; 43 } 44 45 public void setDate(String date) { 46 this.date = date; 47 } 48 49 public String getName() { 50 return name; 51 } 52 53 public void setName(String name) { 54 this.name = name; 55 } 56 57 public persion(){} 58 59 public persion(String name,String date,String time,String local,String temperature,String instruction){ 60 super(); 61 this.name=name; 62 this.date=date; 63 this.temperature=temperature; 64 this.time=time; 65 this.local=local; 66 this.instruction=instruction; 67 } 68 69 @Override 70 public String toString() { 71 return "persion{" + 72 "name='" + name + '\'' + 73 ", date='" + date + '\''+ 74 ", time='" + time + '\''+ 75 ", local='" +local + '\''+ 76 ", temperature='" + temperature + '\''+ 77 ", instruction='" + instruction + '\'' + 78 '}'; 79 } 80 }