[color=darkred][size=x-large]ActiveAndroid可以让你无须写一行SQL语句就可以实现Sqlite数据库的增删改查。执行增删改查操作,你只需要简单地调用onSave()和onDelete()等则可。[/size][/color]
[color=red]下面是作者原话:[/color]
[quote]ActiveAndroid is an active record style ORM (object relational mapper). What does that mean exactly? Well, ActiveAndroid allows you to save and retrieve SQLite database records without ever writing a single SQL statement. Each database record is wrapped neatly into a class with methods like save() and delete().
ActiveAndroid does so much more than this though. Accessing the database is a hassle, to say the least, in Android. ActiveAndroid takes care of all the setup and messy stuff, and all with just a few simple steps of configuration.[/quote]
[size=medium][color=red]github[/color]:[url][color=darkred]https://github.com/pardom/ActiveAndroid[/color][/url][/size]
[size=medium][color=red]library[/color]:[color=darkred]已经将最新的ActiveAndroid封装成jar包(已添加到下方附件),可以直接拿来就用[/color][/size]
[size=x-large]开始ActiveAndroid神奇之旅:[/size]
[color=darkred][size=large]在AndroidManifest.xml中我们需要添加这两个[/size][/color][list]
[*]AA_DB_NAME (数据库名称,这个name不能改,但是是可选的,如果不写的话 是默认的"Application.db"这个值)
[*]AA_DB_VERSION (数据库版本号,同样是可选的 – 默认为1)
[/list]
这个<application>是必须指定的,但你也可以使用自己的Application,继承自com.activeandroid.app.Application
如果你不想或者不能继承com.activeandroid.app.Application的话,那么就这样
ActiveAndroid.initialize(this);做初始化工作,ActiveAndroid.dispose();做清理工作
[color=darkred][size=large]创建数据库模型[/size][/color]
我们使用@Table(name = "Items")来表示表,使用@Column(name = "Name")来表示列,ActiveAndroid会使用自增长的ID作为主键,然后按照注解描述,将类对应映射为数据库表。
[color=darkred][size=large]依赖关系的数据库表[/size][/color]
假如Item和Category是多对一的关系,那么我们可以这样子创建他们的类
[color=darkred][size=large]初始化工作做好,让我们看一下如何保存和更新数据到数据库[/size][/color]
单个插入
保存Category对象
批量插入
如果你要批量插入数据,最好使用事务(transaction)。
使用事务的话只用了 40ms,不然的话需要4秒。
[color=darkred][size=large]删除记录[/size][/color]
我们有三种方式删除一条记录
好吧!是不是感觉so easy!
[color=darkred][size=large]查询数据库[/size][/color]
作者将查询做的非常像SQLite的原生查询语句,几乎涵盖了所有的指令 com.activeandroid.query包下有以下类
[list]
[*]Delete
[*]From
[*]Join
[*]Select
[*]Set
[*]Update
[/list]
看实际例子
对应的sqlite查询语句就是 select * from Item where Category = ? order by RANDOM()
那么下面代码对应的sqlite查询语句作用是什么,尝试去告诉我吧?
[size=large]当然还支持其他非常多的指令,你可以从github上面的测试用例去了解~[/size]
[color=darkred][size=large]最后,咱们来尝试升级数据库[/size][/color]
很多情况下,我们需要在原来数据库的基础上增加字段,而又顾忌用户原来保存的数据,不想删除原来的数据表再重新建表,因此我们需要这样做:
1、首先,你需要更改AndroidManifest.xml中数据库版本号AA_DB_VERSION(必须比上一版本号大的正整数)
2、其次,你需要在assest目录里面创建sql文件,目录结构(/migrations/升级后的版本号.sql),文件里面你需要写上你变动数据库的sql语句(一行一句sql语句),比如我们想在Category表里增加Count字段来统计数量,我们需要这样写:
3、最后,你需要在Category对象里面添加的对应字段(如果有增加字段的话,添加规则参考上面创建数据库模型
[color=red]下面是作者原话:[/color]
[quote]ActiveAndroid is an active record style ORM (object relational mapper). What does that mean exactly? Well, ActiveAndroid allows you to save and retrieve SQLite database records without ever writing a single SQL statement. Each database record is wrapped neatly into a class with methods like save() and delete().
ActiveAndroid does so much more than this though. Accessing the database is a hassle, to say the least, in Android. ActiveAndroid takes care of all the setup and messy stuff, and all with just a few simple steps of configuration.[/quote]
[size=medium][color=red]github[/color]:[url][color=darkred]https://github.com/pardom/ActiveAndroid[/color][/url][/size]
[size=medium][color=red]library[/color]:[color=darkred]已经将最新的ActiveAndroid封装成jar包(已添加到下方附件),可以直接拿来就用[/color][/size]
[size=x-large]开始ActiveAndroid神奇之旅:[/size]
[color=darkred][size=large]在AndroidManifest.xml中我们需要添加这两个[/size][/color][list]
[*]AA_DB_NAME (数据库名称,这个name不能改,但是是可选的,如果不写的话 是默认的"Application.db"这个值)
[*]AA_DB_VERSION (数据库版本号,同样是可选的 – 默认为1)
[/list]
<manifest ...>
<application android:name="com.activeandroid.app.Application" ...>
...
<meta-data android:name="AA_DB_NAME" android:value="Pickrand.db" />
<meta-data android:name="AA_DB_VERSION" android:value="5" />
</application>
</manifest>
这个<application>是必须指定的,但你也可以使用自己的Application,继承自com.activeandroid.app.Application
public class MyApplication extends com.activeandroid.app.Application { ...
如果你不想或者不能继承com.activeandroid.app.Application的话,那么就这样
public class MyApplication extends SomeLibraryApplication {
@Override
public void onCreate() {
super.onCreate();
ActiveAndroid.initialize(this);
}
@Override
public void onTerminate() {
super.onTerminate();
ActiveAndroid.dispose();
}
}
ActiveAndroid.initialize(this);做初始化工作,ActiveAndroid.dispose();做清理工作
[color=darkred][size=large]创建数据库模型[/size][/color]
我们使用@Table(name = "Items")来表示表,使用@Column(name = "Name")来表示列,ActiveAndroid会使用自增长的ID作为主键,然后按照注解描述,将类对应映射为数据库表。
@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;
}
[color=darkred][size=large]依赖关系的数据库表[/size][/color]
假如Item和Category是多对一的关系,那么我们可以这样子创建他们的类
@Table(name = "Items")
public class Item extends Model {
@Column(name = "Name")
public String name;
@Column(name = "Category")
public Category category;
}
@Table(name = "Categories")
public class Category extends Model {
@Column(name = "Name")
public String name;
//方法可选,对外键创建没有影响
public List<Item> items() {
return getMany(Item.class, "Category");
}
[color=darkred][size=large]初始化工作做好,让我们看一下如何保存和更新数据到数据库[/size][/color]
单个插入
保存Category对象
Category restaurants = new Category();
restaurants.name = "Restaurants";
restaurants.save();
批量插入
如果你要批量插入数据,最好使用事务(transaction)。
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();
}
使用事务的话只用了 40ms,不然的话需要4秒。
[color=darkred][size=large]删除记录[/size][/color]
我们有三种方式删除一条记录
Item item = Item.load(Item.class, 1);
item.delete();
Item.delete(Item.class, 1);
new Delete().from(Item.class).where("Id = ?", 1).execute();
好吧!是不是感觉so easy!
[color=darkred][size=large]查询数据库[/size][/color]
作者将查询做的非常像SQLite的原生查询语句,几乎涵盖了所有的指令 com.activeandroid.query包下有以下类
[list]
[*]Delete
[*]From
[*]Join
[*]Select
[*]Set
[*]Update
[/list]
看实际例子
public static Item getRandom(Category category) {
return new Select()
.from(Item.class)
.where("Category = ?", category.getId())
.orderBy("RANDOM()")
.executeSingle();
}
对应的sqlite查询语句就是 select * from Item where Category = ? order by RANDOM()
那么下面代码对应的sqlite查询语句作用是什么,尝试去告诉我吧?
public static List<Item> getAll(Category category) {
return new Select()
.from(Item.class)
.where("Category = ?", category.getId())
.orderBy("Name ASC")
.execute();
}
[size=large]当然还支持其他非常多的指令,你可以从github上面的测试用例去了解~[/size]
[color=darkred][size=large]最后,咱们来尝试升级数据库[/size][/color]
很多情况下,我们需要在原来数据库的基础上增加字段,而又顾忌用户原来保存的数据,不想删除原来的数据表再重新建表,因此我们需要这样做:
1、首先,你需要更改AndroidManifest.xml中数据库版本号AA_DB_VERSION(必须比上一版本号大的正整数)
2、其次,你需要在assest目录里面创建sql文件,目录结构(/migrations/升级后的版本号.sql),文件里面你需要写上你变动数据库的sql语句(一行一句sql语句),比如我们想在Category表里增加Count字段来统计数量,我们需要这样写:
ALTER TABLE Category ADD COLUMN Count INTEGER;
3、最后,你需要在Category对象里面添加的对应字段(如果有增加字段的话,添加规则参考上面创建数据库模型