一、基本介绍:
ActiveAndoid是通过注解与反射将实体类的字段与SQL建立联系。在创建数据库、创建表时不会接触到复杂的SQL语句。
二、基本使用:
1、导入依赖:
在项目根目录gradle加入maven库:
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
在工程gradel加入依赖:
compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'
2、配置数据库名称、版本号、表信息
<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="AA_DB_NAME" android:value="user.db" /> //数据库名
<meta-data android:name="AA_DB_VERSION" android:value="1" /> //数据库版本号
//表信息配置(此配置可省略,但效率会变慢,因为需要遍历所有类寻找Model的子类,所以做好此处配置)
<meta-data android:name="AA_MODELS" android:value="com.hfy.Students" />
</application>
3、初始化ActiveAndroid
在你的Application中初始化:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ActiveAndroid.initialize(this);
}
@Override
public void onTerminate() {
super.onTerminate();
//清理ActiveAndroid
ActiveAndroid.dispose();
}
}
4、创建表:
实体类继承Model即可:
![](/qrcode.jpg)
//此处注解可设置表名,无注解则取类名
@Table(name = "students")
public class Student extends Model {
public Student() {
}
public Student(long sId, String name, String tel_no, int cls_id) {
this.sId = sId;
this.name = name;
this.tel_no = tel_no;
this.cls_id = cls_id;
}
//此处注解可设置字段名以及约束(Model已有默认的id,所以这取sid)
@Column(name = "sid")
private long sId;
@Column(unique = true)
private String name;
@Column(length = 11)
private String tel_no;
@Column
private int cls_id;
}
5、数据库操作,增删改查都简单,
添加一条数据,调用save方法即可:
Student student = new Student();
student.setSId(i);
student.setName("name_" + i);
student.setTel_no("100_" + i);
student.setCls_id(10 * i);
student.save();
删除一条数据:
new Delete().from(Student.class).where("Id = ?", 1).execute();
其余操作类似。
5、升级表:
ActiveAndroid的数据库升级不像其他数据库升级那么简单,下面是ActiveAndroid的版本升级的步骤:
1,先把数据库版本改为要升级到的版本,如把改为2
<meta-data android:name="AA_DB_VERSION" android:value="2" /> //数据库版本号改为2
2,在工程下新建assets/migrations,在该目录下增加一个修改过得版本号,比如我现在的版本号升级为2,则文件夹名为2.sql。
3,在2.sql文件夹下使用sql语句来编写版本升级新增的功能,如新增了一个字段:
ALTER TABLE user ADD COLUMN Count INTEGER;
三、坑点
1、ActiveAndroid 不支持Android Studio 的快速运行instant run,所以在代码调试打apk时要关闭instant run。否则会错误:
activeandroid.TableInfo.getTableName()空指针。
2、在高版本android8.0以上会报错:
java.lang.SecurityException: Failed to find provider null for user 0; expected to find a valid ContentProvider for this authority at android.os.Parcel.readException(Parcel.java:1942) at android.os.Parcel.readException(Parcel.java:1888) at android.content.IContentService$Stub$Proxy.notifyChange(IContentService.java:801) at android.content.ContentResolver.notifyChange(ContentResolver.java:2046) at android.content.ContentResolver.notifyChange(ContentResolver.java:1997) at android.content.ContentResolver.notifyChange(ContentResolver.java:1967) at com.activeandroid.Model.save(Model.java:162)
解决方式是清单中添加即可:
<provider android:name="com.activeandroid.content.ContentProvider" android:authorities="<your.package.name>" android:enabled="true" android:exported="false"> </provider>
另外,