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

适用于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
 9
10
<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
 9
10
11
12
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
 9
10
11
12
13
14
@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
 9
10
11
12
13
14
15
16
@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;
        }
}

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

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

    @Column(name = "Category")
    public Category category;
}

我们需要借助一个方法:

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

    public List<Item> items() {
        return getMany(Item.class, "Category");
    }
}

第三步

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

1
2
3
Category restaurants = new Category();
restaurants.name = "Restaurants";
restaurants.save();

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

1
2
3
4
Item item = new Item();
item.category = restaurants;
item.name = "Outback Steakhouse";
item.save();

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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();
}

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

1
2
3
4
5
6
7
//第一种
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();

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

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

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

1
2
3
4
5
6
7
public static Item getRandom(Category category) {
    return new Select()
        .from(Item.class)
        .where("Category = ?", category.getId())
        .orderBy("RANDOM()")
        .executeSingle();
}

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

1
2
3
4
5
6
7
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值