ActiveAndroid 管理数据库以及ActiveAndroid 如何管理boolean类型

来源:互联网 发布:红蜘蛛软件破解 编辑:程序博客网 时间:2024/06/05 02:47

适用于Android平台的轻量级ORM架构


activeAndroid对于boolean值的处理

 
@Table(name = "notify")public class Notify extends Model implements Serializable {    @Column(name = "userid")    public String userid;    @Column(name = "cmd_code")    public int CMD_CODE;    @Column(name = "title")    public String title;    @Column(name = "content")    public String content;    @Column(name = "timestamp")    public int timestamp;    @Column(name = "isreaded")    public boolean isReaded;    public Notify() {    }    public Notify(String userid, int CMD_CODE, String title, String content, int timestamp, boolean isReaded) {        this.userid = userid;        this.CMD_CODE = CMD_CODE;        this.title = title;        this.content = content;        this.timestamp = timestamp;        this.isReaded = isReaded;    }} 

执行语句


 1 2 
  new Notify("100", 100, "社区通知栏 true", "最新通知!", (int) (new Date().getTime() / 1000), true).save();  new Notify("100", 100, "社区通知栏 false","最新通知!", (int) (new Date().getTime() / 1000), false).save(); 

 


通过sqlite expert查看

DDL:

CREATE TABLE notify (Id INTEGER PRIMARY KEY AUTOINCREMENT, cmd_code INTEGER, content TEXT, isreaded INTEGER, timestamp INTEGER, title TEXT, userid TEXT);

activeandroid  boolean: true:1  false:0


update语句的正确写法:    new Update(Notify.class).set("isReaded=?", 1).where("Id<5").execute();



配置流程:

第一步

配置我们的基本信息:

 1 2 3 4 5 6 7 8 910
<manifest ...>    <application android:name="com.activeandroid.app.Application" ...>        ...        <meta-data android:name="AA_DB_NAME" android:value="数据库名称.db" />        <meta-data android:name="AA_DB_VERSION" android:value="版本数字" />    </application></manifest>

如果你需要定义自己的Application你可以:

1
public class MyApplication extends com.activeandroid.app.Application { ...

如果你自己的Application已经继承了别的库的类你可以:

 1 2 3 4 5 6 7 8 9101112
public class MyApplication extends SomeLibraryApplication {    @Override    public void onCreate() {        super.onCreate();        ActiveAndroid.initialize(this);    }        @Override    public void onTerminate() {        super.onTerminate();        ActiveAndroid.dispose();    }}

第二步

配置完成之后我们创建两张表,我们可以这样:

 1 2 3 4 5 6 7 8 91011121314
@Table(name = "Categories")public class Category extends Model {     @Column(name = "Name")    public String name;}@Table(name = "Items")public class Item extends Model {    @Column(name = "Name")    public String name;    @Column(name = "Category")    public Category category;}

声明表名使用**@Table(name="")**,声明列名使用**@Colnmn(name="")**,这样就ok了。

当我们创建对象需要有参数的构造方法,我们需要下面这样,AciveAndroid 会使用我们的无参的构造方法实例化对象:

 1 2 3 4 5 6 7 8 910111213141516
@Table(name = "Items")public class Item extends Model {    @Column(name = "Name")    public String name;    @Column(name = "Category")    public Category category;        public Item(){                super();        }        public Item(String name, Category category){                super();                this.name = name;                this.category = category;        }}

我们创建一个**一对多**关系,可以这样:

12345678
@Table(name = "Items")public class Item extends Model {    @Column(name = "Name")    public String name;    @Column(name = "Category")    public Category category;}

我们需要借助一个方法:

123456789
@Table(name = "Categories")public class Category extends Model {    @Column(name = "Name")    public String name;    public List<Item> items() {        return getMany(Item.class, "Category");    }}

第三步

保存数据,我们可以这样:

123
Category restaurants = new Category();restaurants.name = "Restaurants";restaurants.save();

创建一个Item,和Categroy存在依赖关系,多个同样

1234
Item item = new Item();item.category = restaurants;item.name = "Outback Steakhouse";item.save();

插入多条数据的时候我们可以使用事务,可以这样:

 1 2 3 4 5 6 7 8 9101112
ActiveAndroid.beginTransaction();try {        for (int i = 0; i < 100; i++) {            Item item = new Item();            item.name = "Example " + i;            item.save()        }        ActiveAndroid.setTransactionSuccessful();}finally {        ActiveAndroid.endTransaction();}

删除一条数据,可以这样:

1234567
//第一种Item item = Item.load(Item.class, 1);item.delete();//第二种Item item = Item.delete(Item.class, 1);//第三种new Delete().from(Item.class).where("Id = ?", 1).execute();

查询数据
随机查出一项,我们可以这样:

123
public static Item getRandom() {    return new Select().from(Item.class).orderBy("RANDOM()").executeSingle();}

根据条件查询,但是得到结果随机的一项,我们可以这样:

1234567
public static Item getRandom(Category category) {    return new Select()        .from(Item.class)        .where("Category = ?", category.getId())        .orderBy("RANDOM()")        .executeSingle();}

下面是我们通常用的一个根据条件查询的:

1234567
public static List<Item> getAll(Context context, Category category) {    return new Select()        .from(Item.class)        .where("Category = ?", category.getId())        .orderBy("Name ASC")        .execute();}

第四步

当我们数据库表,字段改变的时候,我们可以这样:
1.先把数据库版本增加1
2.在**/assets/migrations**目录下面增加一个修改过版本的版本号sql例如**AA_DB_VERSION 是 2**,我们的文件名**2.sql**
sql例如:

1
alter table Items add colour(varchar);









0 0
原创粉丝点击