做安卓开发无可避免的用到数据库,直接自己封装也是可以的,这个下一篇再写,现在先来看看xutils的dbutils的使用方式:
1.下载xutls(网址:https://github.com/wyouflf/xUtils)
2.项目中直接引用或者只是把jar包拷贝进项目的libs文件夹
3.自己封装下,把需要用到的接口封装出来(DatabaseUtils.java)
4.这里给出创建db跟数据库升级的代码,如下:
public static DatabaseUtils getInstance(Context context) {//单例
if (dbUtils == null) {
dbUtils = new DatabaseUtils(context);
}
return dbUtils;
}
//创建数据库以及升级相关代码
public DatabaseUtils(Context context) {
mContext = context;
db = DbUtils.create(mContext, "data.db", curVersion, new DbUpgradeListener() {//curVersion:数据库版本,升级就是根据对比该字段决定要不要执行DbUpgradeListener
@Override
public void onUpgrade(DbUtils arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
try {
if (arg1 == 1 && arg2 == 2) {//判断数据库版本从1到2,满足的话执行下面语句
String sql = "ALTER TABLE " + TableUtils.getTableName(SOSNumber.class)
+ " ADD COLUMN TEMP TEXT";
arg0.execNonQuery(sql);
}
} catch (DbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
// 建议加上注解, 混淆后表名不受影响
@Table(name = "Company")
public class Company implements Serializable {
@Id // 如果主键没有命名名为id或_id的时,需要为主键添加此注解
@NotNull
//@NoAutoIncrement // int,long类型的id默认自增,不想使用自增时取消注释
private int id;
@Column(column = "name")
@NotNull
private String name;
@Column(column = "phone")
@NotNull
private String phone;
public Company() {
//此处很重要,增加了下面的带参构造函数,一定要写无参构造函数,否则该表会创建失败
}
public Company(int id, String name, String phone) {
this.id = id;
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Company{"name='" + getName() + '\'' + ", phone='" + getPhone()+'}';
}
}
DatabaseUtils db = new DatabaseUtils(context);
int id = 1;
String name = "XX有限公司";
String number = "0755-1234567";
Company company = new Company(id, name, number);
//也可以使用下面这种方式
/*
Company company = new Company();
company.setId(id);//貌似在上面类里面少写了这个接口,大家将就下自己加上吧
company.setName(name);
company.setPhone(number);
*/
//saveOrupdate的定义:public void saveOrupdate(Class entityType, Object id, Object entity, List> entities)
db.saveOrupdate(Company.class,id,company,null);
// 建议加上注解, 混淆后表名不受影响
@Table(name = "CompanyInfo")
public class CompanyInfo extends EntityBase implements Serializable{
//只是举例,所以剩下字段还请自行添加
@Foreign(column = "company_id", foreign = "id")//前面id为存在本表里面外键的字段名(company_id),后面id为对应父表主键字段名
public Company parent;
@Column(column = "id")
@NotNull
private int Id;
@Column(column = "company_id")
@NotNull
private int companyId;
public CompanyInfo(){
}
public InsureCompanyInfo(Company parent, int parentid,int id) {
this.parent = parent;
this.companyId = parentid;
this.Id = id;
}
//下面请自行添加一些get,set函数
@Override
public String toString() {
return "CompanyInfo{" +
"id=" + id + +
'}';
}
}
int id = 2;
String name = "XX公司";
String phone = "0755-1234567";
Company cc = new InsureCompany(id,name,phone);
ArrayList fieldList = new ArrayList();
for (int i = 0;i<5;i++) {//这里示例性的写五条数据,暂时id用i来写了
CompanyInfo ef = new CompanyInfo(cc, id, i);
fieldList.add(ef);
}
db.saveOrupdate(Company.class, cc.getId(), cc, fieldList);
int id = 1;
Company cc = db.findFirst(Selector.from(Company.class).where("id", "=", id));//查找第一个id=1的
Company cc1 = db.findById(Company.class,id);//如果id为主键不允许重复,那么这句跟上面那句作用一样
//此外还有:db.findAll(Selector);等接口,查找所有满足条件的数据;保存直接使用db.save()或者db.saveAll(),使用方式都是一样的,就不多写了
db.delete(CompanyInfo.class, WhereBuilder.b("company_id", "=", cc.getId()));//删除CompanyInfo表中父表id = 1的
//db.deleteById(),db.deleteAll()等删除接口,同上使用方式
public void saveOrupdate(Class entityType, Object id, Object entity, List> entities) {
T tmp = findById(entityType, id);
if (tmp == null) {
if (entities != null && entities.size() != 0) {
saveBindingAll(entities);
} else {
save(entity);
}
} else {
update(entity);
if(entities != null){
updateAll(entities);
}
}
}