適用於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
配置流程:
第一步
配置我們的基本信息:
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);
|