PageRenderTime 30ms CodeModel.GetById 11ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/charmmy/dao/PeopleDao.java

http://book-address.googlecode.com/
Java | 152 lines | 123 code | 18 blank | 11 comment | 5 complexity | f0d9ffd4c31d019188b79a9d012b9589 MD5 | raw file
  1package com.charmmy.dao;
  2
  3import static com.charmmy.tools.DefineFinal.PEOPLE_TABLE;
  4
  5import java.util.ArrayList;
  6import java.util.List;
  7
  8import android.content.ContentValues;
  9import android.content.Context;
 10import android.database.Cursor;
 11import android.database.sqlite.SQLiteDatabase;
 12
 13import com.charmmy.pojo.People;
 14import com.charmmy.tools.MySqliteHelper;
 15
 16
 17/**
 18 * 
 19 * @author james
 20 *
 21 */
 22public class PeopleDao {
 23	
 24	private SQLiteDatabase db;
 25	
 26	private MySqliteHelper helper;
 27	
 28	public PeopleDao(Context context) {
 29		helper = new MySqliteHelper(context);
 30	}
 31
 32	//新建联系人
 33	public boolean add(People people) {
 34		db = helper.getWritableDatabase();
 35		ContentValues values = new ContentValues();
 36		values.put("name", people.getName());
 37		values.put("phone", people.getPhone());
 38		values.put("email", people.getEmail());
 39		values.put("tel", people.getTel());
 40		values.put("address", people.getAddress());
 41		values.put("back_content", people.getBackContent());
 42		db.insert(PEOPLE_TABLE, "id", values);
 43		return true;
 44	}
 45	
 46	//删除联系人
 47	public boolean delete(Integer...ids) {
 48		if(ids.length == 0) {
 49			return false;
 50		}
 51		StringBuffer sb = new StringBuffer();
 52		String[] idstr = new String[ids.length];
 53		for(int i=0; i<ids.length; i++) {
 54			sb.append("?").append(",");
 55			idstr[i] = String.valueOf(ids[i]);
 56			
 57		}
 58		sb.deleteCharAt(ids.length);
 59		db = helper.getWritableDatabase();
 60		db.delete(PEOPLE_TABLE, "_id in (" + sb + ")", idstr);
 61		return true;
 62	}
 63	
 64	//更新联系人
 65	public boolean update(People people) {
 66		db = helper.getWritableDatabase();
 67		ContentValues values = new ContentValues();
 68		values.put("name", people.getName());
 69		values.put("phone", people.getPhone());
 70		values.put("email", people.getEmail());
 71		values.put("tel", people.getTel());
 72		values.put("address", people.getAddress());
 73		values.put("back_content", people.getBackContent());
 74		db.update(PEOPLE_TABLE, values, "_id=?",
 75				new String[]{String.valueOf(people.getId())});
 76		return true;
 77	}
 78	
 79	//根据id查询联系人
 80	public People find(int id) {
 81		db = helper.getReadableDatabase();
 82		String[] columns = {"_id", "name", "phone",
 83				"tel", "email", "address", "back_content"};
 84		Cursor cursor = db.query(PEOPLE_TABLE, columns, "_id=?", 
 85				new String[]{String.valueOf(id)}, null, null, null);
 86		if(cursor.moveToNext()) {
 87			People people  = new People();
 88			people.setId(cursor.getInt(0));
 89			people.setName(cursor.getString(1));
 90			people.setPhone(cursor.getString(2));
 91			people.setTel(cursor.getString(3));
 92			people.setEmail(cursor.getString(4));
 93			people.setAddress(cursor.getString(5));
 94			people.setBackContent(cursor.getString(6));
 95			return people;
 96			
 97		} 
 98		return null;
 99	}
100	
101	public Cursor findByName(String name) {
102		db = helper.getReadableDatabase();
103		String[] columns = {"_id","name", "phone",
104				"tel", "email", "address", "back_content"};
105		Cursor cursor = db.query(PEOPLE_TABLE, columns, "name LIKE '"+name+ "%'", 
106				null, null, null, "name ASC");
107		return cursor;
108	}
109	//查询所有联系人
110	public List<People> findAll() {
111		db = helper.getReadableDatabase();
112		String[] columns = {"id", "name", "phone",
113				"tel", "email", "address", "back_content"};
114		Cursor cursor = db.query(PEOPLE_TABLE, columns, null, 
115				null, null, null, "name ASC");
116		List<People> list = new ArrayList<People>();
117		while(cursor.moveToNext()) {
118			People people  = new People();
119			people.setId(cursor.getInt(0));
120			people.setName(cursor.getString(1));
121			people.setPhone(cursor.getString(2));
122			people.setTel(cursor.getString(3));
123			people.setEmail(cursor.getString(4));
124			people.setAddress(cursor.getString(5));
125			people.setBackContent(cursor.getString(6));
126			list.add(people);
127		} 
128		return list;
129	}
130	
131	public Cursor findAllCursor() {
132		db = helper.getReadableDatabase();
133		String[] columns = { "_id","name", "phone",
134				"tel", "email", "address", "back_content"};
135		Cursor cursor = db.query(PEOPLE_TABLE, columns, null, 
136				null, null, null, "name ASC");
137		return cursor;
138	}
139
140	//所有联系人总得条数
141	public int count() {
142		db = helper.getReadableDatabase();
143		String[] columns = {"count(1)"};
144		Cursor cursor = db.query(PEOPLE_TABLE, columns, null, 
145				null, null, null, null);
146		if(cursor.moveToNext()) {
147			int count = cursor.getInt(0);
148			return count;
149		}
150		return 0;
151	}
152}