xUtils系列之DbUtils-查询
来源:互联网 发布:combox控件绑定数据库 编辑:程序博客网 时间:2024/06/05 08:30
其实DbUtils的代码已经很清楚了,所以举个例子 来描述具体用法.
新建一个MainActivity类,在onCreate函数里面可以这样用:
首先,建立一个实体类Person:
package com.example.entities;import com.lidroid.xutils.db.annotation.Column;import com.lidroid.xutils.db.annotation.Table;@Table(name="person")public class Person {private int id;@Column(column="name")private String name;@Column(column="hobby")private String hobby;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getHobby() {return hobby;}public void setHobby(String hobby) {this.hobby = hobby;}@Overridepublic String toString(){return "[id="+id+" name="+name+" hobby="+hobby+"]";}}
新建一个MainActivity类,在onCreate函数里面可以这样用:
@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);ViewUtils.inject(this);initWebView();DbUtils db = DbUtils.create(this);try {db.dropTable(Person.class);} catch (DbException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}String[] names={"zhao","qian","sun","li","zhou","wu","zhen","wang"};String[] hobbys={"pingpong","pingpong","pingpong","football","football","basketball","basketball","basketball"};List<Person> list = new ArrayList<Person>();for(int i=0;i<8;++i){Person p = new Person();p.setName(names[i]);p.setHobby(hobbys[i]);list.add(p);}try {db.saveAll(list);db.findAll(Child.class);db.findAll(Selector.from(Child.class).where("id", ">", 2));db.findById(Child.class, 1);db.findFirst(Child.class);db.findFirst(Selector.from(Child.class).where("id", ">", 2));db.findDbModelAll(DbModelSelector.from(Child.class).groupBy("hobby").having(WhereBuilder.b("id", ">", 2)));db.findDbModelFirst(DbModelSelector.from(Child.class).groupBy("hobby").having(WhereBuilder.b("id", ">", 2)));Cursor c = db.execQuery(new SqlInfo("select * from child;"));//findDbModelFirst和findDbModelAll相当是对execQuery做了再次封装,//返回的DbModel中,封装了通过列名获取value的方法,如:String getString(String columnName)等等.DbModel model = db.findDbModelFirst(new SqlInfo("select * from child;")); //采取原生sql语句,可以做多表查询操作List<DbModel> modellist = db.findDbModelAll(new SqlInfo("select * from child;"));} catch (DbException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
1 0
- xUtils系列之DbUtils-查询
- xUtils系列之DbUtils-概览
- xUtils系列之DbUtils-Id,NoAutoIncrement注解
- xUtils系列之DbUtils-Check注解
- xUtils系列之DbUtils-Column注解
- xUtils系列之DbUtils-Table注解
- xUtils系列之DbUtils-Transient注解
- xUtils系列之DbUtils-Unique注解
- xUtils系列之DbUtils-NotNull注解
- xUtils系列之DbUtils-Foreign注解
- xUtils系列之DbUtils-Finder注解
- xUtils系列之DbUtils-保存自定义类型
- xUtils系列之DbUtils-保存自定义类型
- xUtils框架之DbUtils
- xUtils系列之DbUtils-开启Sqlite3外键约束
- xUtils系列之DbUtils-增,删,更新,替换操作
- XUtils之ViewUtils和DBUtils
- android高级框架xUtils之DbUtils
- kyeremal-poj3468-A simple Problem with Integers-伸展树
- 重构小结——从传参到传实体,从datatable到泛型
- Protocol Buffers for Object-C
- 第十一周 项目0 是春哥啊
- Ural 1586 Threeprime Numbers
- xUtils系列之DbUtils-查询
- 树查找(二)二叉平衡树
- android 打包 eclipse 错误 Export aborted because fatal lint errors were found
- 【c语言】用malloc函数给结构体赋值的使用方法,free清空
- oracle 自动内存管理
- 打印正三角形与倒三角形(C++)
- iOS 获取文本内容的高度
- 简单排序(冒泡、选择、插入)
- ELF format