Import Android.content.context;import Android.database.sqlite.sqlitedatabase;import android.database.sqlite.sqliteopenhelper;/** * Blacklist database */public class Blacknumberopenhelper extends Sqliteopenhelper { C0/>public Blacknumberopenhelper (Context context) { Super (context, "blacknumber.db", NULL, 1); } @Override public void OnCreate (Sqlitedatabase db) { //CREATE TABLE number: phone numbers; mode: Intercept mode, 0 (Intercept phone), 1 (Intercept SMS), 2 (Intercept all) String sql = "CREATE TABLE blacknumber (_id integer PRIMARY key autoincrement, number" + "varchar", Mode Integ ER) "; Db.execsql (SQL); } @Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) { }}
Import Android.content.contentvalues;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Java.util.arraylist;import Cn.itcast.mobilesafe11.db.blacknumberopenhelper;import cn.itcast.mobilesafe11.domain.blacknumberinfo;/** * Created by Administrator on 2016/11/24 0024. * <p/> * blacklist additions and deletions: crud * <p/> * Single case design mode */public class Blacknumberdao {private Final Blacknumberopenhelpe R Mhelper; 3. Declares a static object: Two ways of initializing: 1. A hungry man mode//private static Blacknumberdao minstance = new Blacknumberdao (); 2. Lazy mode private static Blacknumberdao minstance; 1. Private construction Method Private Blacknumberdao (Context ctx) {mhelper = new Blacknumberopenhelper (CTX); }//2. Public method, return singleton object publicly static Blacknumberdao getinstance (Context ctx) {//lazy: Consider thread safety issues in two ways: 1. Method plus Synchronous Lock sy nchronized, low efficiency; 2. Add a synchronous lock to the code block that created the object//read the data without thread safety issues, write data with thread safety issues//a, B, c if (minstance = = null) {//b, C Synchronized (blacknumberdao.class) {//a if (minstance = = null) {m Instance = new Blacknumberdao (CTX); }}} return minstance; } public boolean Add (String number, int mode) {Sqlitedatabase database = Mhelper.getwritabledatabase (); Contentvalues values = new Contentvalues (); Values.put ("number", number); Values.put ("mode", mode); Returns the ID of the inserted record, 1 indicates the failure of long insert = Database.insert ("Blacknumber", null, values); Database.close (); return Insert! =-1; The public boolean Delete (String number) {Sqlitedatabase database = Mhelper.getwritabledatabase (); Returns the number of deleted rows int delete = Database.delete ("Blacknumber", "number=?", New String[]{number}); Database.close (); return delete > 0; public boolean update (String number, int mode) {Sqlitedatabase database = Mhelper.getwritabledatabase (); Contentvalues values = new Contentvalues (); Values.put ("number", number); Values.put ("mode", mode); Returns the number of rows updated int update = Database.update ("Blacknumber", Values, "Number=?", New String[]{number}); Database.close (); return update > 0; }//Query whether a number is in the blacklist database public boolean find (String #) {sqlitedatabase database = Mhelper.getreadabledatabas E (); cursor cursor = database.query ("Blacknumber", NULL, "Number=?", New String[]{number} , NULL, NULL, NULL); Boolean exist = false; if (cursor! = NULL) {if (Cursor.movetonext ()) {///query to the result//return true; exist = true; } cursor.close (); } database.close ();
return exist; }//Query the interception mode for a number//return-1 indicates no results found public int Findmode (String number) {Sqlitedatabase database = Mhelper.getr Eadabledatabase (); cursor cursor = database.query ("Blacknumber", New string[]{"mode"}, "Number=?", New String[]{number}, NULL, NULL, NULL); int mode =-1; if (cursor! = NULL) {if (Cursor.movetonext ()) {///query to the result mode = Cursor.getint (0) ; } cursor.close (); } database.close (); return mode; }//Query all blacklist collection public arraylist<blacknumberinfo> FindAll () {sqlitedatabase database = Mhelper.getreadab Ledatabase (); cursor cursor = database.query ("Blacknumber", New string[]{"number", "mode"}, NULL, NULL, NULL, NULL, NULL) ; arraylist<blacknumberinfo> list = new arraylist<> (); if (cursor! = NULL) {while (Cursor.movetonext ()) { Blacknumberinfo info = new Blacknumberinfo (); String number = cursor.getstring (0); int mode = Cursor.getint (1); Info.number = number; Info.mode = mode; List.add (info); } cursor.close (); } database.close (); return list; }//Paged query blacklist collection//index: The starting position of the query public arraylist<blacknumberinfo> findpart (int index) {sqlitedatabase Database = Mhelper.getreadabledatabase (); SELECT * from Blacknumber limit 0,20//limit x,y:x represents the starting data position; Y represents how many records are queried//sorted by ID in reverse order cursor cursor = database.rawquery ("Select Number,mode from Blacknumber order by _id de SC "+" limit, ", New String[]{index +" "}); arraylist<blacknumberinfo> list = new arraylist<> (); if (cursor! = NULL) {while (Cursor.movetonext ()) {Blacknumberinfo info = new Blacknumberinfo () ; String number = CURSOR.GEtstring (0); int mode = Cursor.getint (1); Info.number = number; Info.mode = mode; List.add (info); } cursor.close (); } database.close (); return list; }//Returns the total number of entries public int gettotalcount () {//select count (*) from blacknumber sqlitedatabase database = Mhelper.getreadabledatabase (); cursor cursor = database.rawquery ("SELECT count (*) from blacknumber", null); int count = 0; if (cursor! = NULL) {if (Cursor.movetonext ()) {count = Cursor.getint (0); } cursor.close (); } database.close (); return count; }}
Example code for SQLite operation