android139 360 黑名單 增刪改查-資料庫操作

來源:互聯網
上載者:User

標籤:

BlackNumberOpenHelper.java

package com.itheima52.mobilesafe.db.dao;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class BlackNumberOpenHelper extends SQLiteOpenHelper{    public BlackNumberOpenHelper(Context context) {        super(context, "safe.db", null, 1);    }    /**     * blacknumber 表名     * _id 主鍵自動成長     * number 電話號碼     * mode 攔截模式 電話攔截 簡訊攔截     */    @Override    public void onCreate(SQLiteDatabase db) {        db.execSQL("create table blacknumber (_id integer primary key autoincrement,number varchar(20),mode varchar(2))");    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    }}

BlackNumberDao.java

package main.java.com.itheima52.mobilesafe.db.dao;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.SystemClock;import com.itheima52.mobilesafe.bean.BlackNumberInfo;import java.util.ArrayList;import java.util.List;public class BlackNumberDao {//增刪改查    public BlackNumberOpenHelper helper;    public BlackNumberDao(Context context) {        helper = new BlackNumberOpenHelper(context);    }    /**添加號碼     * @param number 黑名單號碼     * @param mode   攔截模式     */    public boolean add(String number, String mode) {        SQLiteDatabase db = helper.getWritableDatabase();        ContentValues contentValues = new ContentValues();        contentValues.put("number", number);        contentValues.put("mode", mode);        long rowid = db.insert("blacknumber", null, contentValues);        if (rowid == -1) {            return false;//添加失敗        } else {            return true;        }    }    /**     * 通過電話號碼刪除     * @param number 電話號碼     */    public boolean delete(String number) {        SQLiteDatabase db = helper.getWritableDatabase();        int rowNumber = db.delete("blacknumber", "number=?", new String[]{number});        if (rowNumber == 0) {//刪除的數量            return false;        } else {            return true;        }    }    /**     * 通過電話號碼修改攔截的模式     */    public boolean changeNumberMode(String number, String mode) {        SQLiteDatabase db = helper.getWritableDatabase();        ContentValues values = new ContentValues();        values.put("mode", mode);        int rownumber =db.update("blacknumber", values, "number=?", new String[]{number});//"number=?"是where條件        if (rownumber == 0) {            return false;        } else {            return true;        }    }    /**     * 返回一個黑名單號碼攔截模式     */    public String findNumber(String number) {        String mode = "";        SQLiteDatabase db = helper.getReadableDatabase();        Cursor cursor = db.query("blacknumber", new String[]{"mode"}, "number=?", new String[]{number}, null, null, null);        if (cursor.moveToNext()) {            mode = cursor.getString(0);        }        cursor.close();        db.close();        return mode;    }    /**     * 查詢所有的黑名單     */    public List<BlackNumberInfo> findAll() {        SQLiteDatabase db = helper.getReadableDatabase();        List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();        Cursor cursor = db.query("blacknumber", new String[]{"number", "mode"}, null, null, null, null, null);        while (cursor.moveToNext()) {            BlackNumberInfo blackNumberInfo = new BlackNumberInfo();            blackNumberInfo.setNumber(cursor.getString(0));            blackNumberInfo.setMode(cursor.getString(1));            blackNumberInfos.add(blackNumberInfo);        }        cursor.close();        db.close();        SystemClock.sleep(3000);        return blackNumberInfos;    }    /**     * 分頁載入資料     * @param pageNumber 表示當前是哪一頁     * @param pageSize   表示每一頁有多少條資料     * @return limit 表示限制當前有多少資料     * offset 表示跳過 從第幾條開始     */    public List<BlackNumberInfo> findPar(int pageNumber, int pageSize) {        SQLiteDatabase db = helper.getReadableDatabase();        Cursor cursor = db.rawQuery("select number,mode from blacknumber limit ? offset ?", new String[]{String.valueOf(pageSize),                String.valueOf(pageSize * pageNumber)});        List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();        while (cursor.moveToNext()) {            BlackNumberInfo blackNumberInfo = new BlackNumberInfo();            blackNumberInfo.setMode(cursor.getString(1));            blackNumberInfo.setNumber(cursor.getString(0));            blackNumberInfos.add(blackNumberInfo);        }        cursor.close();        db.close();        return blackNumberInfos;    }    /**     * 分批載入資料     * @param startIndex  開始的位置     * @param maxCount    每頁展示的最大的條目     */    public List<BlackNumberInfo> findPar2(int startIndex, int maxCount) {        SQLiteDatabase db = helper.getReadableDatabase();        Cursor cursor = db.rawQuery("select number,mode from blacknumber limit ? offset ?", new String[]{String.valueOf(maxCount),                String.valueOf(startIndex)});        List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>();        while (cursor.moveToNext()) {            BlackNumberInfo blackNumberInfo = new BlackNumberInfo();            blackNumberInfo.setMode(cursor.getString(1));            blackNumberInfo.setNumber(cursor.getString(0));            blackNumberInfos.add(blackNumberInfo);        }        cursor.close();        db.close();        return blackNumberInfos;    }    /**     * 擷取總的記錄數     */    public int getTotalNumber(){        SQLiteDatabase db = helper.getReadableDatabase();        Cursor cursor = db.rawQuery("select count(*) from blacknumber", null);        cursor.moveToNext();        int count = cursor.getInt(0);        cursor.close();        db.close();        return count;    }}

測試TestBlackNumberDao.java

package com.itheima52.mobilesafe;import android.content.Context;import android.test.AndroidTestCase;import com.itheima52.mobilesafe.bean.BlackNumberInfo;import com.itheima52.mobilesafe.db.dao.BlackNumberDao;import java.util.List;import java.util.Random;public class TestBlackNumberDao extends AndroidTestCase {    public Context mContext;    @Override    protected void setUp() throws Exception {        this.mContext = getContext();        super.setUp();    }    public void testAdd(){        BlackNumberDao dao = new BlackNumberDao(mContext);        Random random = new Random();        for (int i = 0; i < 200; i++) {            Long number = 13300000000l +i;            dao.add(number +"",String.valueOf(random.nextInt(3) + 1));        }    }    public void testDelete(){        BlackNumberDao dao = new BlackNumberDao(mContext);        boolean delete = dao.delete("13300000000");        assertEquals(true,delete);    }    public void testFind(){        BlackNumberDao dao = new BlackNumberDao(mContext);        String number = dao.findNumber("13300000004");        System.out.println(number);    }    public void testFindAll(){        BlackNumberDao dao = new BlackNumberDao(mContext);        List<BlackNumberInfo> blackNumberInfos = dao.findAll();        for (BlackNumberInfo blackNumberInfo : blackNumberInfos) {            System.out.println(blackNumberInfo.getMode() + "" + blackNumberInfo.getNumber());        }    }}

 

android139 360 黑名單 增刪改查-資料庫操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.