1.首先下载xUtils
2.建立数据库单例
**
* Created by Lampo on 16/4/22.
*/
public class DBSingle implements DbUtils.DbUpgradeListener {
private static volatile DBSingle single;
private final DbUtils db;
public DBSingle(Context context) {
db = DbUtils.create(context, Constant.CON_MICRO_CHAT.DB_NAME, Constant.CON_MICRO_CHAT.DB_VERSION, this);
db.configDebug(DevUtils.isDebug());
db.configAllowTransaction(true);
try {
createTables();
} catch (DbException e) {
DevUtils.d(DBSingle.class.getSimpleName(), e.getMessage());
}
}
public static void init(Context context) {
if (single != null)
return;
synchronized (DBSingle.class) {
if (single == null)
single = new DBSingle(context);
}
}
public static DBSingle self() {
if (single == null)
throw new IllegalStateException("请初始化,调用com.mynetworktest..DBSingle.init(Context context)");
return single;
}
private void createTables() throws DbException {
db.createTableIfNotExist(User.class);
db.createTableIfNotExist(ISession.class);
db.createTableIfNotExist(IMessage.class);
db.createTableIfNotExist(IQueryhistory.class);
}
public DbUtils DB() {
return db;
}
@Override
public void onUpgrade(DbUtils dbUtils, int i, int i1) {
}
}
3.创建数据库表以及表参数实体类
/**
* Created by Lampo on 16/5/3.
*/
@Table(name = "queryhistory")//表名字
public class IQueryhistory implements Parcelable, Comparable<IQueryhistory> {
@Id//自增
private int id;
@NotNull//不能为空
private String phone;
//查询:1 ;搜索:2;
private int type;
/**
* 最后更新时间 格式标准时间描述
*/
private long updated_at;
public IQueryhistory() {
}
public IQueryhistory(Parcel in) {
id = in.readInt();
phone = in.readString();
type = in.readInt();
updated_at = in.readLong();
}
public static final Creator<IQueryhistory> CREATOR = new Creator<IQueryhistory>() {
@Override
public IQueryhistory createFromParcel(Parcel in) {
return new IQueryhistory(in);
}
@Override
public IQueryhistory[] newArray(int size) {
return new IQueryhistory[size];
}
};
@Override
public int compareTo(IQueryhistory another) {
if (another == null)
return 1;
// 为何不能直接返回-del?long数值范围多于int,逻辑上可能越界
long del = updated_at - another.updated_at;
if (del > 0)
return 1;
else if (del < 0)
return -1;
else
return 0;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(phone);
dest.writeInt(type);
dest.writeLong(updated_at);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public long getUpdated_at() {
return updated_at;
}
public void setUpdated_at(long updated_at) {
this.updated_at = updated_at;
}
}
4.创建一个方法类 以便对数据库表数据进行修改
/**
* 创建一条新的数据
*/
public static boolean createOrUpdate(String phone, int type) throws DbException {
if (TextUtils.isEmpty(phone) && (type != 1 || type != 2))
return false;
IQueryhistory iQueryhistory = DBSingle.self().DB().findFirst(Selector.from(IQueryhistory.class).where("phone", "=", phone));
//如果表里面没有相同的数据 新建一条数据
if (iQueryhistory == null) {
IQueryhistory queryhistory = new IQueryhistory();
queryhistory.setPhone(phone);
queryhistory.setType(type);
queryhistory.setUpdated_at(System.currentTimeMillis());
DBSingle.self().DB().saveBindingId(queryhistory);
DBSingle.self().DB().saveOrUpdate(queryhistory);
} else {
//如果已经有相约的数据 着修改
updateTime(iQueryhistory);
iQueryhistory.setUpdated_at(System.currentTimeMillis());
DBSingle.self().DB().saveOrUpdate(iQueryhistory);
}
return true;
}
/**
* 删除表中所有数据(清空表)
*
* @throws DbException
*/
public static void deteleAll() throws DbException {
DBSingle.self().DB().delete(IQueryhistory.class);
}
/**
* 删除表中 类型(type)对于 1 的所有数据
*
* @throws DbException
*/
public static void deleteSearchhistoryAll() throws DbException {
DBSingle.self().DB().delete(IQueryhistory.class, WhereBuilder.b("type", "=", 2));
}
/**
* 修改单一参数
*
* @param iQueryhistory
* @throws DbException
*/
public static void updateTime(IQueryhistory iQueryhistory) throws DbException {
DBSingle.self().DB().update(iQueryhistory, "updated_at");
}
/**
* 修改多个参数
*
* @param iQueryhistory
* @throws DbException
*/
public static void updateTimeAndType(IQueryhistory iQueryhistory) throws DbException {
DBSingle.self().DB().update(iQueryhistory, "updated_at", "type");
}
/**
* 获取表中所有数据
*
* @return
* @throws DbException
*/
public static List<IQueryhistory> getList() throws DbException {
return DBSingle.self().DB().findAll(IQueryhistory.class);
}
/**
* 获取表中所有数据 并按时间(updated_at)排序
* true 正序
*
* @return list
* @throws DbException
*/
public static List<IQueryhistory> getListOrderBy() throws DbException {
return DBSingle.self().DB().findAll(Selector.from(IQueryhistory.class).orderBy("updated_at", true));
}
/**
* 获取表中所有 类型(type)等于 1 的数据 并按时间(updated_at)排序
* true 正序
*
* @return list
* @throws DbException
*/
public static List<IQueryhistory> getQueryhistoryList() throws DbException {
return DBSingle.self().DB().findAll(Selector.from(IQueryhistory.class).where("type", "=", 1).orderBy("updated_at", true));
}