1数据库的bean
package org.heima.mobilesafe01.bean;
public class BlackInfo {
public int _id;//
public String phone;
public int _type;
@Override
public String toString() {
return "BlackInfo [_id=" + _id + ", phone=" + phone + ", _type="
+ _type + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + _type;
result = prime * result + ((phone == null) ? 0 : phone.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BlackInfo other = (BlackInfo) obj;
if (_type != other._type)
return false;
if (phone == null) {
if (other.phone != null)
return false;
} else if (!phone.equals(other.phone))
return false;
return true;
}
}
2 数据库的配置类
package org.heima.mobilesafe01.db;
public interface BlackListDB {
String BLACK_DB = "black.db";
int VERSION = 1;
public interface BlackList {
String TABLE_NAME="blacklist";
String ID = "_id";
String PHONE = "phone";
String TYPE = "type";
String CREATE_TABLE_SQL = "create table "+TABLE_NAME+"(" + ID
+ " integer primary key autoincrement," + PHONE + " text,"
+ TYPE + " integer)";
}
}
3 数据库的创建更新类
package org.heima.mobilesafe01.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* @author U
* 黑名单数据库的帮助类
*/
public class BlackDBHelper extends SQLiteOpenHelper {
public BlackDBHelper(Context context) {
super(context, BlackListDB.BLACK_DB, null, BlackListDB.VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// create table blacklist _id,phone,type
db.execSQL("create table blacklist(_id integer primary key autoincrement,phone text,type integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
4 数据库的Dao类
package org.heima.mobilesafe01.db;
import java.util.ArrayList;
import java.util.List;
import org.heima.mobilesafe01.bean.BlackInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class BlackDao {
private BlackDBHelper mDbHelper;
public BlackDao(Context context) {
mDbHelper = new BlackDBHelper(context);
}
// add
public boolean addBlack(BlackInfo info) {
return addBlack(info.phone, info._type);
}
public boolean addBlack(String phone, int type) {
SQLiteDatabase database = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(BlackListDB.BlackList.PHONE, phone);
values.put(BlackListDB.BlackList.TYPE, type);
long insert = database.insert(BlackListDB.BlackList.TABLE_NAME, null,
values);
database.close();
return insert != -1;
}
// delete
public boolean delete(String phone) {
SQLiteDatabase database = mDbHelper.getWritableDatabase();
int delete = database.delete(BlackListDB.BlackList.TABLE_NAME,
"phone=?", new String[] { phone });
database.close();
return delete != 0;
}
// update
public boolean update(String phone,int type){
SQLiteDatabase database = mDbHelper.getWritableDatabase();
// update tableName set type=? where phone=?
ContentValues values = new ContentValues();
values.put(BlackListDB.BlackList.TYPE, type);
int update=database.update(BlackListDB.BlackList.TABLE_NAME, values, "phone=?", new String[]{phone});
database.close();
return update != 0;
}
// select
public List<BlackInfo> queryAllBlackInfos(){
List<BlackInfo> blackInfos=new ArrayList<BlackInfo>();
SQLiteDatabase database = mDbHelper.getWritableDatabase();
Cursor cursor = database.rawQuery("select _id,phone,type from blacklist", null);
if(cursor!=null){
BlackInfo info=null;
while(cursor.moveToNext()){
info=new BlackInfo();
info._id=cursor.getInt(0);
info.phone=cursor.getString(1);
info._type=cursor.getInt(2);
blackInfos.add(info);
}
cursor.close();
}
database.close();
return blackInfos;
}
public int getCount(){
SQLiteDatabase database = mDbHelper.getWritableDatabase();
Cursor cursor =database.rawQuery("select count(1) from blacklist",null);
if(cursor!=null){
if(cursor.moveToNext()){
return cursor.getInt(0);
}
cursor.close();
}
database.close();
return -1;
}
/**
* 删除所有
*/
public void deleteAll(){
SQLiteDatabase database = mDbHelper.getWritableDatabase();
database.execSQL("delete from blacklist");
database.close();
}
public List<BlackInfo> queryPartBlackInfos(int count,int offset){
List<BlackInfo> blackInfos=new ArrayList<BlackInfo>();
SQLiteDatabase database = mDbHelper.getWritableDatabase();
Cursor cursor = database.rawQuery("select _id,phone,type from blacklist limit ? offset ?", new String[]{String.valueOf(count),String.valueOf(offset)});
if(cursor!=null){
BlackInfo info=null;
while(cursor.moveToNext()){
info=new BlackInfo();
info._id=cursor.getInt(0);
info.phone=cursor.getString(1);
info._type=cursor.getInt(2);
blackInfos.add(info);
}
cursor.close();
}
database.close();
return blackInfos;
}
/**
* @param phone
* @return
* 通过电话号码查询他的拦截类型
*/
public int getType(String phone){
SQLiteDatabase database = mDbHelper.getWritableDatabase();
Cursor cursor =database.rawQuery("select type from blacklist where phone=?",new String[]{phone});
if(cursor!=null){
if(cursor.moveToNext()){
return cursor.getInt(0);
}
cursor.close();
}
database.close();
return -1;
}
}
5 单元测试
package org.heima.mobilesafe01.test;
import java.util.List;
import org.heima.mobilesafe01.bean.BlackInfo;
import org.heima.mobilesafe01.db.BlackDao;
import org.heima.mobilesafe01.utils.L;
import android.test.AndroidTestCase;
public class BlackDaoTest extends AndroidTestCase {
public void testAdd() {
BlackDao blackDao = new BlackDao(getContext());
for (int i = 0; i < 50; i++) {
if(i%2==0){
blackDao.addBlack("100" + i, 1);
}else{
blackDao.addBlack("100" + i, 2);
}
}
}
public void testQuery(){
BlackDao blackDao = new BlackDao(getContext());
List<BlackInfo> queryAllBlackInfos = blackDao.queryAllBlackInfos();
for (BlackInfo info:queryAllBlackInfos) {
L.d(info.toString());
}
}
public void testUpdate(){
BlackDao blackDao = new BlackDao(getContext());
blackDao.update("1001", 1);
}
public void testDelete(){
BlackDao blackDao = new BlackDao(getContext());
blackDao.delete("1000");
}
public void testDeleteAll(){
BlackDao blackDao = new BlackDao(getContext());
blackDao.deleteAll();
}
public void testQueryPart(){
BlackDao blackDao = new BlackDao(getContext());
List<BlackInfo> queryAllBlackInfos = blackDao.queryPartBlackInfos(5, 10);
for (BlackInfo info:queryAllBlackInfos) {
L.d(info.toString());
}
}
public void testGetCount(){
BlackDao blackDao = new BlackDao(getContext());
int count = blackDao.getCount();
L.d("count:"+count);
}
}