binbinyang博客----关于xutils---db 数据库操作

来源:互联网 发布:围棋数字软件 编辑:程序博客网 时间:2024/06/05 06:16

先不说,xutils这个开发框架怎么样...但是从进公司第一天开始,他们就开始用了.所以没办法

这次说的是 DbUtils模块:

  • android中的orm框架,一行代码就可以进行增删改查;
  • 支持事务,默认关闭;
  • 可通过注解自定义表名,列名,外键,唯一性约束,NOT NULL约束,CHECK约束等(需要混淆的时候请注解表名和列名);
  • 支持绑定外键,保存实体时外键关联实体自动保存或更新;
  • 自动加载外键关联实体,支持延时加载;
  • 支持链式表达查询,更直观的查询语义,参考下面的介绍或sample中的例子。

需要的权限

<uses-permissionandroid:name="android.permission.INTERNET"/>

    <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

需要的权限

<uses-permissionandroid:name="android.permission.INTERNET"/>

    <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


创建数据库

  DaoConfig config = new DaoConfig(context);

  config.setDbName("xUtils-demo"); //db名

  config.setDbVersion(1);  //db版本

  DbUtils db = DbUtils.create(config);//db还有其他的一些构造方法,比如含有更新表版本的监听器的


创建表

  db.createTableIfNotExist(User.class); //创建一个表User

    db.save(user);//在表中保存一个user对象。最初执行保存动作时,也会创建User表


删除表

  db.dropTable(User.class); 


开启事务

  db.configAllowTransaction(true);


db相关Annotation

  @Check    check约束
  @Column   列名
  @Finder   一对多、多对一、多对多关系(见sample的Parent、Child中的使用)
  @Foreign  外键
  @Id       主键,当为int类型时,默认自增。 非自增时,需要设置id的值
  @NoAutoIncrement  不自增
  @NotNull  不为空
  @Table    表名
  @Transient  不写入数据库表结构
  @Unique   唯一约束


下图是 项目的运行图..点击添加购物车.这个动作 是用的本地数据库.把添加的商品,放入本地数据库中,不进行后台的接口交互.至于这么做的目的大家懂的. 其实淘宝的安卓版本也是这样...用本地化



先说表 跟字段 怎么 写吧

/** * @author yangbin *  */@Table(name = "shoppingCartTable")public class ShoppingCartTable implements Parcelable {List<ShoppingCartTable> list;public static String MemMemberId = "memmemberid";public static String ShopProductProjectId = "shopproductprojectid";// @Id 主键,当为int类型时,默认自增。 非自增时,需要设置id的值private int id;@Column(column = "agencyid")private int agencyid;// 机构ID@Column(column = "shopname")private String shopname;// 机构名字@Column(column = "shopproductprojectid")private int shopproductprojectid;// 商品ID@Column(column = "projectname")private String projectname;// 商品名字@Column(column = "countquantity")private int countquantity;// 商品数量@Column(column = "price")private double price;// 商品价格@Column(column = "memmemberid")private int memmemberid;// 会员ID@Column(column = "img")private String img;// 图片字段public boolean selectAllShop;public boolean selectALLStore;public ShoppingCartTable() {db = BaseApplication.db;}public ShoppingCartTable(int id, int agencyid, String shopname,int shopproductprojectid, String projectname, int countquantity,double price, int memmemberid, String img) {super();this.id = id;this.agencyid = agencyid;this.shopname = shopname;this.shopproductprojectid = shopproductprojectid;this.projectname = projectname;this.countquantity = countquantity;this.price = price;this.memmemberid = memmemberid;this.img = img;}DbUtils db;public int getAgencyid() {return agencyid;}public void setAgencyid(int agencyid) {this.agencyid = agencyid;}public String getShopname() {return shopname;}public void setShopname(String shopname) {this.shopname = shopname;}public int getShopproductprojectid() {return shopproductprojectid;}public void setShopproductprojectid(int shopproductprojectid) {this.shopproductprojectid = shopproductprojectid;}public String getProjectname() {return projectname;}public void setProjectname(String projectname) {this.projectname = projectname;}public int getCountquantity() {return countquantity;}public void setCountquantity(int countquantity) {this.countquantity = countquantity;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getImg() {return img;}public void setImg(String img) {this.img = img;}public int getMemmemberid() {return memmemberid;}public void setMemmemberid(int memmemberid) {this.memmemberid = memmemberid;}@Overridepublic int describeContents() {// TODO Auto-generated method stubreturn 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeInt(this.id);dest.writeString(this.shopname);dest.writeInt(this.shopproductprojectid);dest.writeString(this.projectname);dest.writeInt(this.countquantity);dest.writeDouble(this.price);dest.writeInt(this.memmemberid);}}

很多人会疑问;为什么这个地方 我要用 
<pre name="code" class="java">implements Parcelable {  
<pre name="code" class="java">@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeInt(this.id);dest.writeString(this.shopname);dest.writeInt(this.shopproductprojectid);dest.writeString(this.projectname);dest.writeInt(this.countquantity);dest.writeDouble(this.price);dest.writeInt(this.memmemberid);}

//因为 我后面要用意图传递对象数据. 所以用了这个  

先说说 保存的方法 . 其实就是保存到对象里面

/** * 保存商品到数据库 */private void getAddShoppingCart1() {ShoppingCartTable sb = new ShoppingCartTable();sb.setAgencyid(bean.Message.AgencyId);sb.setShopname(bean.Message.AgencyName);sb.setShopproductprojectid(bean.Message.ShopProductProjectId);sb.setProjectname(bean.Message.ProjectName);sb.setCountquantity(bean.Message.Quantity);sb.setPrice(bean.Message.Preferential);sb.setImg(bean.Message.ProjectImages);sb.setMemmemberid(userBean.getMemMemberId());new ShoppingCartTable().saveBean(sb, ShopProductProjectId);}

然后在说说查询

/** * 查询 根据会员ID 查询当前购物车有哪些 */public List<ShoppingCartTable> getAllByDate(int memmemberid) {List<ShoppingCartTable> list;if (BaseApplication.db != null) {try {list = BaseApplication.db.findAll(Selector.from(ShoppingCartTable.class).where(ShoppingCartTable.MemMemberId, "=", memmemberid));return list;} catch (DbException e) {e.printStackTrace();return null;}} else {return null;}}

说个题外话 如果你想进行复杂的查询 可以看我下面这个代码 这是我另外一个模块用到的数据库

/** * 如果摇一摇前查出有数据.就要去拿最近一次测试的数据的时间比较 *  * @return */public LifeStyleTable  lastTimeDate(int typeid, int memmemberid,long nowdate) {if (BaseApplication.db != null) {try {boolean desc = true;a = BaseApplication.db.findFirst(Selector.from(LifeStyleTable.class).where(LifeStyleTable.TypeId, "=", typeid).and("memmemberid", "=", memmemberid).and("nowdate", "=", nowdate).orderBy("time", desc));return a;} catch (DbException e) {e.printStackTrace();return null;}} else {return null;}}

如果你要用到groupBy,可以看下面的代码..

/** * 查看记录 一共有多少天里面有数据 */public List<DbModel> findDay(int memmemberid, long nowdate ) {if (BaseApplication.db != null) {try {dbModels = db.findDbModelAll(Selector.from(LifeStyleTable.class).groupBy("nowdate").select("nowdate", "time", "sum(score)","sum(typeid=1)", "sum(typeid=2)","sum(typeid=3)", "sum(typeid=4)","sum(typeid=5)") );} catch (DbException e) {e.printStackTrace();return null;}} else {return null;}return dbModels;}

然后验证是否成功 就需 导出数据库了  切记 手机要ROOT




0 0