ActiveAndroid是一個(gè)輕量級(jí)的ORM框架,可以以類的方式簡(jiǎn)單快捷地進(jìn)行數(shù)據(jù)庫(kù)的管理,而無需編寫一個(gè)單獨(dú)的SQL語(yǔ)句。
ActiveAndroid git地址
CSDN同步發(fā)布
配置
1、AndroidManifeset中添加如下配置:
<application
...
android:name="com.activeandroid.app.Application"
>
<meta-data
android:name="AA_DB_NAME"
android:value="xxx.db" />
<meta-data
android:name="AA_DB_VERSION"
android:value="7" />
<meta-data
android:name="AA_MODELS"
android:value="com.syd.oden.odendemo.entity.sqltab.LocationTab, com.syd.oden.odendemo.entity.sqltab.MusicFavorTab" />
AA_MODELS為數(shù)據(jù)庫(kù)中表的實(shí)體類
2、Application繼承com.activeandroid.app.Application
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();
}
}
3、創(chuàng)建表
@Table(name = "PictureTabs")
public class PictureTab extends Model {
private static MyLog myLog = new MyLog("[PictureTab] ");
@Column(name = "dirName")
String dirName;
@Column(name = "fileName")
String fileName;
@Column(name = "describe")
String describe;
@Column(name = "longitude")
double longitude;
@Column(name = "latitude")
double latitude;
public PictureTab() {
super();
}
public PictureTab(String dirName, String fileName, double longitude, double latitude) {
super();
this.dirName = dirName;
this.fileName = fileName;
this.longitude = longitude;
this.latitude = latitude;
}
}
增刪改查
增
for (int i=0; i<5; i++) {
DbBlesGroup dbBleGroup = new DbBlesGroup();
dbBleGroup.groupIndex = i;
dbBleGroup.groupName = "groupName" + i;
dbBleGroup.addr = "addr" + i;
dbBleGroup.name = "name" + i;
dbBleGroup.save();
}
查
查出所有
List<DbBlesGroup> dbBleGroupList = new ArrayList<>();
dbBleGroupList = new Select()
.from(DbBlesGroup.class)
.orderBy("groupName ASC")
.execute();
for (int i=0; i<dbBleGroupList.size(); i++)
{
L.d("dbBleGroupList :" + dbBleGroupList.get(i).groupName);
}
指定條件查找
List<DbBlesGroup> dbBleGroupList = new ArrayList<>();
dbBleGroupList = new Select()
.from(DbBlesGroup.class)
.where("groupName = ?", "groupName3")
.orderBy("groupName ASC")
.execute();
多條件查找
newSelect().from(UserViewTab.class).where("viewId=? and bleAddr=?",viewId,addr).executeSingle();
使用事務(wù)(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();
}
.orderBy("id DESC")降序
.orderBy("id ASC")升序
刪除
new Delete().from(DbBlesGroup.class).where("groupName = ?", "groupName2").execute();
改
new Update(DbBlesGroup.class).set("addr = ?", "123").where("groupName = ?", "groupName2").execute();
也可直接用save修改
注意事項(xiàng)
1、構(gòu)造方法中記得加入super();
2、在sudio2.2運(yùn)行報(bào)錯(cuò)解決:
erro: 'java.lang.String com.activeandroid.TableInfo.getTableName()' on a null object reference.
關(guān)掉Instant Run

3、表中包含另一個(gè)表,則保存的時(shí)候要先保存另一個(gè)表;
發(fā)現(xiàn)一個(gè)bug,表中包含另一個(gè)表,查另一個(gè)表里的數(shù)據(jù)可能有誤
recipeAlarmList.add(RecipeAlarmTab.getById(recipeTab.getRecipeAlarmTab1().getId())); //activeAndroid貌似有bug,故通過ID重新查詢一次