一、基本介紹:
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即可:
//此處註解可設置表名,無註解則取類名
@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>
另外,