/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

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