码迷,mamicode.com
首页 > 数据库 > 详细

23.创建黑名单数据库

时间:2015-10-30 18:21:37      阅读:457      评论:0      收藏:0      [点我收藏+]

标签:

1.创建帮助类,创建表
  1. public class BlackNumberOpenHelper extends SQLiteOpenHelper{
  2. public BlackNumberOpenHelper(Context context) {
  3. super(context, "safe.db", null, 1);
  4. }
  5. /**
  6. * blacknumber 表名
  7. * _id 主键自动增长
  8. * number 电话号码
  9. * mode 拦截模式 电话拦截 短信拦截
  10. * @param db
  11. */
  12. @Override
  13. public void onCreate(SQLiteDatabase db) {
  14. db.execSQL("create table blacknumber (_id integer primary key autoincrement,number varchar(20),mode varchar(2))");
  15. }
  16. @Override
  17. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  18. }
  19. }
2.创建数据库,并写增删改查、分页加载、分批加载、获取总记录方法
  1. public class BlackNumberDao {
  2. public BlackNumberOpenHelper helper;
  3. public BlackNumberDao(Context context) {
  4. helper = new BlackNumberOpenHelper(context);
  5. }
  6. /**
  7. * @param number 黑名单号码
  8. * @param mode 拦截模式
  9. */
  10. public boolean add(String number, String mode) {
  11. SQLiteDatabase db = helper.getWritableDatabase();
  12. ContentValues contentValues = new ContentValues();
  13. contentValues.put("number", number);
  14. contentValues.put("mode", mode);
  15. long rowid = db.insert("blacknumber", null, contentValues);
  16. if (rowid == -1) {
  17. return false;
  18. } else {
  19. return true;
  20. }
  21. }
  22. /**
  23. * 通过电话号码删除
  24. *
  25. * @param number 电话号码
  26. */
  27. public boolean delete(String number) {
  28. SQLiteDatabase db = helper.getWritableDatabase();
  29. int rowNumber = db.delete("blacknumber", "number=?", new String[]{number});
  30. if (rowNumber == 0) {
  31. return false;
  32. } else {
  33. return true;
  34. }
  35. }
  36. /**
  37. * 通过电话号码修改拦截的模式
  38. *
  39. * @param number
  40. */
  41. public boolean changeNumberMode(String number, String mode) {
  42. SQLiteDatabase db = helper.getWritableDatabase();
  43. ContentValues values = new ContentValues();
  44. values.put("mode", mode);
  45. int rownumber =db.update("blacknumber", values, "number=?", new String[]{number});
  46. if (rownumber == 0) {
  47. return false;
  48. } else {
  49. return true;
  50. }
  51. }
  52. /**
  53. * 返回一个黑名单号码拦截模式
  54. *
  55. * @return
  56. */
  57. public String findNumber(String number) {
  58. String mode = "";
  59. SQLiteDatabase db = helper.getReadableDatabase();
  60. Cursor cursor = db.query("blacknumber", new String[]{"mode"}, "number=?", new String[]{number}, null, null, null);
  61. if (cursor.moveToNext()) {
  62. mode = cursor.getString(0);
  63. }
  64. cursor.close();
  65. db.close();
  66. return mode;
  67. }
  68. /**
  69. * 查询所有的黑名单
  70. *
  71. * @return
  72. */
  73. public List<BlackNumberInfo> findAll() {
  74. SQLiteDatabase db = helper.getReadableDatabase();
  75. List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
  76. Cursor cursor = db.query("blacknumber", new String[]{"number", "mode"}, null, null, null, null, null);
  77. while (cursor.moveToNext()) {
  78. BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
  79. blackNumberInfo.setNumber(cursor.getString(0));
  80. blackNumberInfo.setMode(cursor.getString(1));
  81. blackNumberInfos.add(blackNumberInfo);
  82. }
  83. cursor.close();
  84. db.close();
  85. SystemClock.sleep(3000);
  86. return blackNumberInfos;
  87. }
  88. /**
  89. * 分页加载数据
  90. *
  91. * @param pageNumber 表示当前是哪一页
  92. * @param pageSize 表示每一页有多少条数据
  93. * @return limit 表示限制当前有多少数据
  94. * offset 表示跳过 从第几条开始
  95. */
  96. public List<BlackNumberInfo> findPar(int pageNumber, int pageSize) {
  97. SQLiteDatabase db = helper.getReadableDatabase();
  98. // 数值转为字符串:string.valueof()
  99. // 也可以字符串转为数值:Folat.valueof(str).folatValue() Double.valueof(str).doubleValue()
  100. Cursor cursor = db.rawQuery("select number,mode from blacknumber limit ? offset ?", new String[]{String.valueOf(pageSize),
  101. String.valueOf(pageSize * pageNumber)});
  102. List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
  103. while (cursor.moveToNext()) {
  104. BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
  105. blackNumberInfo.setMode(cursor.getString(1));
  106. blackNumberInfo.setNumber(cursor.getString(0));
  107. blackNumberInfos.add(blackNumberInfo);
  108. }
  109. cursor.close();
  110. db.close();
  111. return blackNumberInfos;
  112. }
  113. /**
  114. * 分批加载数据
  115. * @param startIndex 开始的位置
  116. * @param maxCount 每页展示的最大的条目
  117. * @return
  118. */
  119. public List<BlackNumberInfo> findPar2(int startIndex, int maxCount) {
  120. SQLiteDatabase db = helper.getReadableDatabase();
  121. Cursor cursor = db.rawQuery("select number,mode from blacknumber limit ? offset ?", new String[]{String.valueOf(maxCount),
  122. String.valueOf(startIndex)});
  123. List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();
  124. while (cursor.moveToNext()) {
  125. BlackNumberInfo blackNumberInfo = new BlackNumberInfo();
  126. blackNumberInfo.setMode(cursor.getString(1));
  127. blackNumberInfo.setNumber(cursor.getString(0));
  128. blackNumberInfos.add(blackNumberInfo);
  129. }
  130. cursor.close();
  131. db.close();
  132. return blackNumberInfos;
  133. }
  134. /**
  135. * 获取总的记录数
  136. * @return
  137. */
  138. public int getTotalNumber(){
  139. SQLiteDatabase db = helper.getReadableDatabase();
  140. Cursor cursor = db.rawQuery("select count(*) from blacknumber", null);
  141. cursor.moveToNext();
  142. int count = cursor.getInt(0);
  143. cursor.close();
  144. db.close();
  145. return count;
  146. }
  147. }
3.新建测试类
  1. public class TestBlackNumberDao extends AndroidTestCase {
  2. public Context mContext;
  3. @Override
  4. protected void setUp() throws Exception {
  5. this.mContext = getContext();
  6. super.setUp();
  7. }
  8. public void testAdd(){
  9. BlackNumberDao dao = new BlackNumberDao(mContext);
  10. Random random = new Random();
  11. for (int i = 0; i < 200; i++) {
  12. Long number = 13300000000l +i;
  13. dao.add(number +"",String.valueOf(random.nextInt(3) + 1));
  14. }
  15. }
  16. public void testDelete(){
  17. BlackNumberDao dao = new BlackNumberDao(mContext);
  18. boolean delete = dao.delete("13300000000");
  19. assertEquals(true,delete);
  20. }
  21. public void testFind(){
  22. BlackNumberDao dao = new BlackNumberDao(mContext);
  23. String number = dao.findNumber("13300000004");
  24. System.out.println(number);
  25. }
  26. public void testFindAll(){
  27. BlackNumberDao dao = new BlackNumberDao(mContext);
  28. List<BlackNumberInfo> blackNumberInfos = dao.findAll();
  29. for (BlackNumberInfo blackNumberInfo : blackNumberInfos) {
  30. System.out.println(blackNumberInfo.getMode() + "" + blackNumberInfo.getNumber());
  31. }
  32. }
  33. }













23.创建黑名单数据库

标签:

原文地址:http://www.cnblogs.com/liuyu0529/p/4923684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!