虽然android已经提供了管理数据库的工具类,但是对一些同学学习起来还是有些困难的,今天我就介绍一下ActiveAndroid。
ActiveAndroid是一个活性的记录的风格ORM(对象关系映射)。这是什么意思呢?那么,ActiveAndroid允许你保存和检索SQLite数据库记录,而无需编写一个单独的SQL语句。每个数据库记录被包裹整齐地归为一类,如save()和delete()的方法。
ActiveAndroid这样做远不止这一点,虽然。访问数据库是一件麻烦事,至少可以说,在Android。 ActiveAndroid照顾所有的设置和凌乱的东西,所有的配置,只需几个简单的步骤。
配置我们的基本信息:
<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>
public class MyApplication extends 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(); } }
配置完成之后我们创建两张表,我们可以这样:
@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 会使用我们的无参的构造方法实例化对象:
@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; } }
我们创建一个**一对多**关系,可以这样:
@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"); } }
保存数据,我们可以这样:
Category restaurants = new Category(); restaurants.name = "Restaurants"; restaurants.save();
Item item = new Item(); item.category = restaurants; item.name = "Outback Steakhouse"; item.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(); }
//第一种 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();
public static Item getRandom() { return new Select().from(Item.class).orderBy("RANDOM()").executeSingle(); }
public static Item getRandom(Category category) { return new Select() .from(Item.class) .where("Category = ?", category.getId()) .orderBy("RANDOM()") .executeSingle(); }
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例如:
alter table Items add colour(varchar);