Android開發——數據庫框架Suger遇到的大坑(Gson和Suger的複用Bean請見“大坑三”)
大坑一
自己寫了一個Demo按照官網以及GitHub上Suger的使用步驟完整走下來,Demo閃退,以插入數據爲例
User user = new User("liao","123");
user.setUserName("liao");
user.setPassWord("789");
user.save();
Log.i("test",String.valueOf(user.save()));
User是一個bean,調試時一到user.save();
立馬掛掉,而且看不到報錯信息。看了別人的博客發現是由於沒有將自己的Application和Suger綁定,解決方法如下:
步驟一:
寫一個類繼承Application
第一種寫法:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SugarContext.init(this);
}
@Override
public void onTerminate() {
super.onTerminate();
SugarContext.terminate();
}
}
第二種寫法:
public class MyApplication extends SugarApp {
@Override
public void onCreate() {
super.onCreate();
SugarContext.init(this);
}
@Override
public void onTerminate() {
super.onTerminate();
}
}
步驟二:
在AndroidManifast.xml文件中進行綁定
<application
...
android:name="com.example.lj_xwl.sugerdb.MyApplication">
</application>
大坑二
Demo運行仍然掛掉,但是有報錯信息了
<!--創建數據庫-->
<meta-data android:name="DATABASE" android:value="test.db"/>
<meta-data android:name="VERSION" android:value="2"/>
<meta-data android:name="QUERY_LOG" android:value="true"/>
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example.lj_xwl.sugerdb.bean"/>
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sugerdemo/com.sugerdemo.MainActivity}: android.database.sqlite.SQLiteException: Can't downgrade database from version 3 to 2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
原來是版本問題 將版本改爲3就可以 只能從低往高改 不能從高往低,若要改爲低版本,解決辦法是卸載工程、clean重新編譯。
大坑三
服務器要傳id,客戶端存這個字段時,id值與Suger裏存的id值有衝突。其實就是Gson和Suger複用Bean問題。
Suger的id爲int和long類型,在自己的bean中設置的getId0會有衝突:
long id;
public long getId() {
return id;
}
Error:(14, 17) 錯誤: User中的getId()無法覆蓋SugarRecord中的getId()
返回類型long與Long不兼容
若要存這個id且不和Suger的id衝突,網上的方法是用Gson中的@SerializedName註解,分別用Suger官網的兩種用法如下:
方法一:
public class UserBackInfo extends SugarRecord{
// @Unique
@Expose
@SerializedName("id")
Long id; //用戶id
@Expose
String uname;//用戶姓名
@Expose
String upwd;//密碼
@Expose
String uphone;//手機(用戶名)
public UserBackInfo(){
}
public UserBackInfo(Long id, String uname, String upwd, String uphone) {
this.id = id;
this.uname = uname;
this.upwd = upwd;
this.uphone = uphone;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
方法二:
@Table
public class UserBackInfo{
// @Unique
@Expose
@SerializedName("id")
Long id; //用戶id
@Expose
String uname;//用戶姓名
@Expose
String upwd;//密碼
@Expose
String uphone;//手機(用戶名)
public UserBackInfo(){
}
public UserBackInfo(Long id, String uname, String upwd, String uphone) {
this.id = id;
this.uname = uname;
this.upwd = upwd;
this.uphone = uphone;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
調試後發現,方法一程序掛掉,方法二完美完成數據庫操作,實現了Gson和Suger的複用Bean問題。
注意:
1、 Gson的@Expose要註解在每一個變量中
2、@SerializedName(“id”)是解決複用問題的關鍵
3、複用Bean後查詢從服務器拿來的Gson數據的Id是多少Suger裏存的就是多少,比如我接到的id是25且只插入了這一條,那麼如下可以查到數據
UserBackInfo quary = SugarRecord.findById(UserBackInfo.class,25);
如下不能查到:
UserBackInfo quary = SugarRecord.findById(UserBackInfo.class,1);
因爲數據庫中第一條數據就是id爲25的,沒有id爲1的。從而驗證了Gson和Suger確實複用了Bean。