[关闭]
@Faith 2014-12-25T12:23:50.000000Z 字数 2891 阅读 2605

ActiveAndroid使用 基于AndroidStudio

已发表


配置模块的build.gradle

repositories {
mavenCentral()
mavenLocal()
maven { url "https://oss.sonatype.org/content/
repositories/snapshots/" }
}

`dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:20.0.0'
compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'
}`

继承application

如果你的项目之前没有其他的application要继承,你可以直接继承此application。

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

如果有的话,可以在onCreatel里调下初始化方法,在onTerminate里做清理。
`public class MyApplication extends SomeLibraryApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        ActiveAndroid.initialize(this);
    }
    @Override
public void onTerminate() {
    super.onTerminate();
    ActiveAndroid.dispose();
}
}`

配置AndroidManifest.xml

<manifest ...>
<application android:name="MyApplication" ...>
...
<meta-data android:name="AA_DB_NAME" android:value="your.db" />
<meta-data android:name="AA_DB_VERSION" android:value="5" />
</application>
</manifest>

AA_DB_NAME (这个name不能改,但是是可选的,如果不写的话 是默认的”Application.db”这个值)

AA_DB_VERSION (optional – defaults to 1)


创建表

@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;
}

表都继承自Model类,表里可以关联表。Table是表明,Column是字段。


插入单条数据

Category category = new Category();
category.name = "categoryname";
category.save();


插入一组数据

最好用下事务.

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


查询单条

都是用Select对象是查询,和sql语句差不多,from、where、orderby等都支持。executeSingle表示要一条返回数据。

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


查询一组数据

直接用execute取就是一组数据。

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


删除记录

这几种方法都可以

Item item = Item.load(Item.class, 1);
item.delete();

Item.delete(Item.class, 1);
new Delete().from(Item.class).


更新

save方法也可以做更新。一般是做单条更新。

Foo for = Foo.load(Foo.class, 1);//1 is the id
foo.bar = "new value";
foo.save();

批量更新就需要用update对象了,语法和sql类似,也是set和where组成。

new Update(SomeModel.class)
.set("Enabled = 0")
.where("Account = ?", account.getId())
.execute();


数据库升级

如果想给现有的表增加或删除column,这个时候只需要把sql脚本放在/assets/migrations文件夹下,然后需要:

增加数据库版本,即增加配置文件下的application种的AA_DB_VERSION

在/assets/migrations文件夹下提供一个NewVersion.sql脚本

ActiveAndroid将会自动执行大于database-version的sql脚本文件

假设需要给Items表增加一个“colour”列,那么需要把AA_DB_VERSION增加到2,并且在/assets/migrations目录下提供一个2.sql的脚本文件,内容如下

ALTER TABLE Items ADD COLUMN Color INTEGER;


开源地址

https://github.com/pardom/ActiveAndroid/wiki/Getting-started


缺陷

目前只发现沒有清空表里所有數據的方法,需要自己寫SQL。
其实用些功能没有对象化也没关系,可以通过写sql补充的。
无所谓啦。


ProGuard

-keep public class com.activeandroid.**

-keep public class * extends com.activeandroid.ActiveRecordBase

-keepattributes Column

-keepattributes Table

-keepattributes Annotation

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注