xutils中dbutils的基础使用详解
来源:互联网 发布:java中类的修饰符 编辑:程序博客网 时间:2024/05/17 03:00
转载:http://blog.csdn.net/z358247505/article/details/45971313
做安卓开发无可避免的用到数据库,直接自己封装也是可以的,这个下一篇再写,现在先来看看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() {
- @Override
- public void onUpgrade(DbUtils arg0, int arg1, int arg2) {
-
- try {
- if (arg1 == 1 && arg2 == 2) {
- String sql = "ALTER TABLE " + TableUtils.getTableName(SOSNumber.class)
- + " ADD COLUMN TEMP TEXT";
- arg0.execNonQuery(sql);
- }
- } catch (DbException e) {
-
- e.printStackTrace();
- }
- }
- });
- }
5.创建实体类(也就是定义一下即将要存储在数据库中的表结构),这里给出简单的例子:一个Company表-
- @Table(name = "Company")
- public class Company implements Serializable {
-
- <span style="white-space:pre"> </span>@Id
- @NotNull
-
- 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()+'}';
- }
- }
6.创建表- DatabaseUtils db = new DatabaseUtils(context);
-
- int id = 1;
- String name = "XX有限公司";
- String number = "0755-1234567";
- Company company = new Company(id, name, number);
-
-
-
-
-
-
-
-
- db.saveOrupdate(Company.class,id,company,null);
7.很多时候需要使用外键关联表,这时候的表结构需要重新定义,字表代码如下(父表请参考上面):-
- @Table(name = "CompanyInfo")
- public class CompanyInfo extends EntityBase implements Serializable{
-
-
- @Foreign(column = "company_id", foreign = "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;
- }
-
-
- @Override
- public String toString() {
- return "CompanyInfo{" +
- "id=" + id + +
- '}';
- }
- }
8.带外键的数据保存,代码如下:- int id = 2;
- String name = "XX公司";
- String phone = "0755-1234567";
- Company cc = new InsureCompany(id,name,phone);
- ArrayList<CompanyInfo> fieldList = new ArrayList<CompanyInfo>();
- for (int i = 0;i<5;i++) {
- CompanyInfo ef = new CompanyInfo(cc, id, i);
- fieldList.add(ef);
- }
- db.saveOrupdate(Company.class, cc.getId(), cc, fieldList);
9.数据查找跟删除,代码如下:- int id = 1;
- Company cc = db.findFirst(Selector.from(Company.class).where("id", "=", id));
- Company cc1 = db.findById(Company.class,id);
-
- db.delete(CompanyInfo.class, WhereBuilder.b("company_id", "=", cc.getId()));
-
差点忘记了,saveOrupdate接口是自己封装的,也放上来吧- public <T> void saveOrupdate(Class<T> 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);
- }
- }
- }
1 0