ActiveAndroid 管理数据库

来源:http://blog.csdn.net/wangjia55/article/details/11257271

虽然android已经提供了管理数据库的工具类,但是对一些同学学习起来还是有些困难的,今天我就介绍一下ActiveAndroid。
ActiveAndroid是一个活性的记录的风格ORM(对象关系映射)。这是什么意思呢?那么,ActiveAndroid允许你保存和检索 SQLite数据库记录,而无需编写一个单独的SQL语句。每个数据库记录被包裹整齐地归为一类,如save()和delete()的方法。

ActiveAndroid这样做远不止这一点,虽然。访问数据库是一件麻烦事,至少可以说,在Android。 ActiveAndroid照顾所有的设置和凌乱的东西,所有的配置,只需几个简单的步骤。

第一步

配置我们的基本信息:

 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);

库文件下载地址:http://www.eoeandroid.com/forum.php?mod=attachment&aid=ODYyOTd8NDI1Mzk3NjJ8MTM1ODQ0MjA4Mnw2NDU4MjB8MjQ5MjAx

发表评论