【1】设计数据表 IPhoneRecordTable
/**
* 数据表
*
* @author wangzhuobin
*
*/
public class IPhoneRecordTable {
// 表名
public final static String TABLE_NAME = "phoneRecord";
// 字段名
public final static String ID = "_id";
public final static String NAME = "name";
public final static String PHONE = "phone";
public final static String FROMPLACE = "fromPlace";
public final static String TOPLACE = "toPlace";
public final static String TIME = "time";
public final static String USERID = "user_id";
// 创建表SQL语句
public final static String CREATE_SQL = "CREATE TABLE " + TABLE_NAME + " ("
+ ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + NAME + " TEXT,"
+ PHONE + " TEXT," + FROMPLACE + " TEXT," + TOPLACE + " TEXT," + TIME
+ " TEXT," + USERID + " TEXT" + ")";
}
(2) 创建表(实现两个方法:onCreate: 创建 onUpgrade:更新 )
/**
* 通话记录SQLiteOpenHelper
*
* @author wangzhuobin
*
*/
public class PhoneRecordOpenHelper extends SQLiteOpenHelper {
private final static int PHONE_DB_VERSION = 2;
private final static String PHONE_DB_NAME = "phoneRecord.db";
private Context mContext;
public PhoneRecordOpenHelper(Context context) {
super(context, PHONE_DB_NAME, null, PHONE_DB_VERSION);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(IPhoneRecordTable.CREATE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
(3) 提供 增删改查 的
/**
* 通话记录的DataModel 提供增删查改数据库操作
*
* @author wangzhuobin
*
*/
public class PhoneRecordDataModel {
private static PhoneRecordDataModel sSelf = null;
private Context mContext;
private PhoneRecordOpenHelper mHelper;
private PhoneRecordDataModel(Context context) {
mContext = context;
mHelper = new PhoneRecordOpenHelper(context);
}
public static synchronized PhoneRecordDataModel getInstance(Context context) {
if (sSelf == null) {
sSelf = new PhoneRecordDataModel(context);
}
return sSelf;
}
public synchronized Cursor query(String table, String[] columns,
String selection, String[] selectionArgs, String groupBy,
String having, String orderBy) {
if (mHelper != null) {
SQLiteDatabase db = mHelper.getReadableDatabase();
return db.query(table, columns, selection, selectionArgs, groupBy,
having, orderBy);
}
return null;
}
public synchronized long insert(String table, String nullColumnHack,
ContentValues values) {
if (mHelper != null) {
SQLiteDatabase db = mHelper.getWritableDatabase();
return db.insert(table, nullColumnHack, values);
}
return -1;
}
public synchronized int delete(String table, String whereClause,
String[] whereArgs) {
if (mHelper != null) {
SQLiteDatabase db = mHelper.getWritableDatabase();
return db.delete(table, whereClause, whereArgs);
}
return 0;
}
public synchronized int update(String table, ContentValues values,
String whereClause, String[] whereArgs) {
if (mHelper != null) {
SQLiteDatabase db = mHelper.getWritableDatabase();
return db.update(table, values, whereClause, whereArgs);
}
return 0;
}
public long addPhoneRecordToDb(OrderBean orderBean) {
return insert(IPhoneRecordTable.TABLE_NAME, null,
PhoneRecordDataParser.parsePhoneInfoToContentValues(orderBean));
}
/**
* 根据userid从本地db获取全部推送消息的方法
*
* @return
*/
public ArrayList<OrderBean> getAllPhoneRecordFromDB(String userId) {
ArrayList<OrderBean> orderBeans = new ArrayList<>();
String selection = null;
if (userId == null) {
selection = IPhoneRecordTable.USERID + " is NULL" ;
} else {
selection = IPhoneRecordTable.USERID + " = " + userId;
}
Cursor cursor = query(IPhoneRecordTable.TABLE_NAME, null, selection, null,
null, null, IPhoneRecordTable.ID + " DESC"); // 倒序排列
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
OrderBean orderBean = PhoneRecordDataParser
.parsePhoneRecordFromCursor(cursor);
if (orderBean != null) {
orderBeans.add(orderBean);
}
} while (cursor.moveToNext());
}
}
if (cursor != null) {
cursor.close();
}
return orderBeans;
}
/**
* 根据 手机号码 记录
*
* @param
*/
public int deletBrowseRecordFromDb(String mobile) {
// DELETE FROM Person WHERE LastName = 'Wilson'
return delete(IBrowseRecordTable.TABLE_NAME, IBrowseRecordTable.USERID + "=?", new String[]{mobile});
}
/**
* 从本地db删除信息的方法
*
* @param orderBeans
*/
public int deletPhoneRecordFromDb(ArrayList<OrderBean> orderBeans) {
if (orderBeans == null || orderBeans.isEmpty()) {
return 0;
}
StringBuffer stringBuffer = new StringBuffer();
for (OrderBean messageInfo : orderBeans) {
if (messageInfo == null) {
continue;
}
if (stringBuffer.length() > 0) {
stringBuffer.append(" or ");
}
stringBuffer.append(IPhoneRecordTable.ID + " = " + messageInfo.getId());
}
return delete(IPhoneRecordTable.TABLE_NAME, stringBuffer.toString(), null);
}
}
(4)数据解析器
/**
* 通话记录的数据解释器
*
* @author wangzhuobin
*
*/
public class PhoneRecordDataParser {
/**
* 解释 orderBean 到contentvalues的方法
*
* @param orderBean
* @return
*/
public static ContentValues parsePhoneInfoToContentValues(OrderBean orderBean) {
if (orderBean == null) {
return null;
}
ContentValues contentValues = new ContentValues();
contentValues.put(IPhoneRecordTable.NAME, orderBean.getPublisherName());
contentValues.put(IPhoneRecordTable.PHONE, orderBean.getPublisherPhone());
contentValues.put(IPhoneRecordTable.FROMPLACE, orderBean.getFromPlace());
contentValues.put(IPhoneRecordTable.TOPLACE, orderBean.getToPlace());
contentValues.put(IPhoneRecordTable.TIME, orderBean.getCallTime());
contentValues.put(IMessageTable.USERID, orderBean.getUserId());
return contentValues;
}
/**
* 从cursor解释messageinfo的方法
*
* @param cursor
* @return
*/
public static OrderBean parsePhoneRecordFromCursor(Cursor cursor) {
if (cursor == null) {
return null;
}
OrderBean orderBean = new OrderBean();
orderBean.setId(cursor.getInt(cursor
.getColumnIndex(IPhoneRecordTable.ID)));
orderBean.setPublisherName(cursor.getString(cursor
.getColumnIndex(IPhoneRecordTable.NAME)));
orderBean.setPublisherPhone(cursor.getString(cursor
.getColumnIndex(IPhoneRecordTable.PHONE)));
orderBean.setFromPlace(cursor.getString(cursor
.getColumnIndex(IPhoneRecordTable.FROMPLACE)));
orderBean.setToPlace(cursor.getString(cursor
.getColumnIndex(IPhoneRecordTable.TOPLACE)));
orderBean.setCallTime(cursor.getString(cursor
.getColumnIndex(IPhoneRecordTable.TIME)));
orderBean.setUserId(cursor.getString(cursor
.getColumnIndex(IPhoneRecordTable.USERID)));
return orderBean;
}
}
网友评论