android 操作sqllite 数据库

来源:互联网 发布:批量部署windows 编辑:程序博客网 时间:2024/05/16 13:00
[java] view plaincopyprint?
  1. package com.b509.activity.patient.mydoctors.service;  
  2.   
  3. public class Person {  
  4.     private Integer id;  
  5.     private String time;  
  6.     private String describe;  
  7.     private String illness;  
  8.     private byte[] pic;  
  9.     public Integer getId() {  
  10.         return id;  
  11.     }  
  12.     public void setId(Integer id) {  
  13.         this.id = id;  
  14.     }  
  15.     public String getTime() {  
  16.         return time;  
  17.     }  
  18.     public void setTime(String time) {  
  19.         this.time = time;  
  20.     }  
  21.     public String getDeccribe() {  
  22.         return describe;  
  23.     }  
  24.     public void setDeccribe(String deccribe) {  
  25.         this.describe = deccribe;  
  26.     }  
  27.     public String getIllness() {  
  28.         return illness;  
  29.     }  
  30.     public void setIllness(String illness) {  
  31.         this.illness = illness;  
  32.     }  
  33.       
  34.       
  35. public byte[] getPic() {  
  36.         return pic;  
  37.     }  
  38.     public void setPic(byte[] pic) {  
  39.         this.pic = pic;  
  40.     }  
  41. public Person(){  
  42.       
  43. }  
  44.       
  45.     public Person(Integer id, String time,String describe,String illness,byte[] pic) {  
  46.         this.id = id;  
  47.         this.time = time;  
  48.         this.describe=describe;  
  49.         this.illness=illness;  
  50.         this.pic = pic;  
  51.     }  
  52.     public Person( String time,String describe,String illness) {  
  53.           
  54.         this.time = time;  
  55.         this.describe=describe;  
  56.         this.illness=illness;  
  57.           
  58.     }  
  59.   
  60. }  
[java] view plaincopyprint?
  1. package com.b509.activity.patient.mydoctors.service;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.content.ContentValues;  
  7. import android.content.Context;  
  8. import android.database.Cursor;  
  9. import android.database.sqlite.SQLiteDatabase;  
  10.   
  11. public class OtherPersonService {  
  12.     private DBOpenHelper dbOpenHelper;  
  13.   
  14.     public OtherPersonService(Context context) {  
  15.         this.dbOpenHelper = new DBOpenHelper(context);  
  16.     }  
  17.   
  18.     // ILLid time describe illness  
  19.   
  20.     public void save(Person person) {  
  21.         SQLiteDatabase db = dbOpenHelper.getWritableDatabase();  
  22.         ContentValues values = new ContentValues();  
  23.         values.put("ILLid", person.getId());  
  24.         values.put("time", person.getTime());  
  25.         values.put("describe", person.getDeccribe());  
  26.         values.put("illness", person.getIllness());  
  27.   
  28.         db.insert("ILLNESS"null, values);  
  29.         db.close();  
  30.     }  
  31.   
  32.     // SQLiteDatabase db = dbOpenHelper.getWritableDatabase();  
  33.   
  34.     public void update(Person person) {  
  35.         // update person set name =? where personid =?  
  36.         SQLiteDatabase db = dbOpenHelper.getWritableDatabase();  
  37.         ContentValues values = new ContentValues();  
  38.         values.put("time", person.getTime());  
  39.         values.put("describe", person.getDeccribe());  
  40.         values.put("illness", person.getIllness());  
  41.         System.out.println(person.getDeccribe());  
  42.         System.out.println(person.getIllness());  
  43.         System.out.println(person.getTime());  
  44.         System.out.println(person.getId());  
  45.         Integer i = person.getId();  
  46.         String id_str = String.valueOf(i);  
  47.         System.out.println(id_str + "把id转化为一个字符串");  
  48.   
  49.         db.update("ILLNESS", values, "ILLid=?"new String[] { id_str });  
  50.         db.close();  
  51.     }  
  52.   
  53.     public void delete(Integer id) {  
  54.         SQLiteDatabase db = dbOpenHelper.getWritableDatabase();  
  55.         db.delete("ILLNESS""ILLid=?"new String[] { id.toString() });  
  56.         db.close();  
  57.     }  
  58.   
  59.     public List<Person> find2() {  
  60.         // 如果只对数据进行读取,建议使用此方法  
  61.         SQLiteDatabase db = dbOpenHelper.getReadableDatabase();  
  62.         Cursor cursor = db.query("ILLNESS"new String[] { "time""illness" },  
  63.                 nullnullnullnull"time");  
  64.         List<Person> persons = new ArrayList<Person>();  
  65.         // select personid,name from person where personid=? order by ... limit  
  66.         // 3,5  
  67.         if (cursor.moveToFirst()) {  
  68.   
  69.             String timeill = cursor.getString(cursor.getColumnIndex("time"));  
  70.   
  71.             String kindsill = cursor  
  72.                     .getString(cursor.getColumnIndex("illness"));  
  73.             System.out.println(timeill);  
  74.   
  75.             System.out.println(kindsill);  
  76.             Person person = new Person();  
  77.   
  78.             person.setTime(timeill);  
  79.             person.setIllness(kindsill);  
  80.             persons.add(person);  
  81.   
  82.             db.close();  
  83.             return persons;  
  84.         }  
  85.         return null;  
  86.     }  
  87.   
  88.     public Person find3(int id) {  
  89.         // 如果只对数据进行读取,建议使用此方法  
  90.         String str_id = id + "";  
  91.         SQLiteDatabase db = dbOpenHelper.getReadableDatabase();  
  92.         Cursor cursor = db.query("ILLNESS"new String[] { "time""illness",  
  93.                 " describe" }, str_id, nullnullnull"time");  
  94.   
  95.         Person person = null;  
  96.         if (cursor.moveToFirst()) {  
  97.   
  98.             String timeill = cursor.getString(cursor.getColumnIndex("time"));  
  99.   
  100.             String kindsill = cursor  
  101.                     .getString(cursor.getColumnIndex("illness"));  
  102.             String disill = cursor.getString(cursor.getColumnIndex("describe"));  
  103.             System.out.println(timeill);  
  104.   
  105.             System.out.println(kindsill);  
  106.             person = new Person();  
  107.   
  108.             person.setTime(timeill);  
  109.             person.setIllness(kindsill);  
  110.             person.setDeccribe(disill);  
  111.               
  112.             return person;  
  113.         }  
  114.         db.close();  
  115.         return null;  
  116.     }  
  117.   
  118.     public List<Person> find() {  
  119.         // 如果只对数据进行读取,建议使用此方法  
  120.         SQLiteDatabase db = dbOpenHelper.getReadableDatabase();  
  121.         Cursor cursor = db.query("ILLNESS"new String[] { "ILLid""time",  
  122.                 "illness" }, nullnullnullnull"time");  
  123.   
  124.         List<Person> persons = new ArrayList<Person>();  
  125.         // select personid,name from person where personid=? order by ... limit  
  126.         // 3,5  
  127.         if (cursor.moveToFirst()) {  
  128.   
  129.             Person person = new Person();  
  130.             System.out.println("---------------");  
  131.             String timeill = cursor.getString(cursor.getColumnIndex("time"));  
  132.             Integer idill = cursor.getInt(cursor.getColumnIndex("ILLid"));  
  133.             String kindsill = cursor  
  134.                     .getString(cursor.getColumnIndex("illness"));  
  135.             System.out.println(timeill);  
  136.             System.out.println(idill);  
  137.             System.out.println(kindsill);  
  138.   
  139.             person.setTime(timeill);  
  140.             person.setIllness(kindsill);  
  141.             persons.add(person);  
  142.             db.close();  
  143.             return persons;  
  144.         }  
  145.         return null;  
  146.     }  
  147.   
  148.     public List<Person> getScrollData(Integer offset, Integer maxResult) {  
  149.   
  150.         List<Person> persons = new ArrayList<Person>();  
  151.         SQLiteDatabase db = dbOpenHelper.getReadableDatabase();  
  152.         Cursor cursor = db.query("ILLNESS"new String[] { "ILLid""time",  
  153.                 "illness" }, nullnullnullnull"time", offset + ","  
  154.                 + maxResult);  
  155.   
  156.         while (cursor.moveToNext()) {  
  157.             int personid = cursor.getInt(cursor.getColumnIndex("ILLid"));  
  158.             String time_ill = cursor.getString(cursor.getColumnIndex("time"));  
  159.             String ill = cursor.getString(cursor.getColumnIndex("illness"));  
  160.             Person p = new Person();  
  161.             p.setId(personid);  
  162.             p.setIllness(ill);  
  163.             p.setTime(time_ill);  
  164.             persons.add(p);  
  165.         }  
  166.         cursor.close();  
  167.         db.close();  
  168.         return persons;  
  169.     }  
  170.   
  171.     public long getCount() {// select count(*) from person  
  172.         SQLiteDatabase db = dbOpenHelper.getReadableDatabase();  
  173.         /* 
  174.          * Cursor cursor = db.query("ILLNESS", new String[] { "count(*)" }, 
  175.          * null, null, null, null, null); 
  176.          */  
  177.         Cursor cursor = db.query("ILLNESS"new String[] { "Illid" }, null,  
  178.                 nullnullnullnull);  
  179.   
  180.         db.close();  
  181.         return cursor.getCount();  
  182.     }  
  183.   
0 0