我正在尝试使用Room persistence db构建一个应用程序,我想获取存储在Business表中的数据并开始使用它。 这是我的DAO
@Dao
public interface BusinessDao {
@Query("SELECT * FROM Business")
Business getAll();
@Query("SELECT business_name, business_phone, business_address, business_manager FROM Business")
public List<BusinessModel> getTuple();
@Query("UPDATE Business SET business_name = :businessName, business_address = :businessAddress, business_phone = :businessPhone, business_manager = :businessManager WHERE bid = :bid")
void updateBusinessById(String businessName, String businessAddress, String businessPhone, String businessManager, int bid);
@Insert
void insertAll(Business... businesses);
@Delete
void delete(Business business);
}
更新,插入和删除均可,但是getAll和getTuple始终返回null。 这是我的实体
@Entity(tableName = "Business")
public class Business extends BaseObservable implements Serializable {
@PrimaryKey(autoGenerate = true)
private int bid;
@ColumnInfo(name = "business_name")
private String businessName;
@ColumnInfo(name = "business_address")
private String businessAddress;
@ColumnInfo(name = "business_phone")
private String businessPhone;
@ColumnInfo(name = "business_manager")
private String businessManager;
@Ignore
private boolean displayError;
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
@Bindable
public boolean isDisplayError() {
return displayError;
}
public void setDisplayError(boolean displayError) {
this.displayError = displayError;
notifyPropertyChanged(BR.displayError);
}
@Bindable
public String getBusinessName() {
if (businessName == null)
return "";
return businessName;
}
@NonNull
public String getBusinessName1() {
if (businessName == null)
return "";
return businessName;
}
@Bindable({"displayError", "businessName"})
public String getBusinessNameError() {
if (!isDisplayError()) {
return "";
}
if (getBusinessName().isEmpty()) {
return "BUSINESS NAME IS EMPTY!";
}
return "";
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
notifyPropertyChanged(BR.businessName);
}
.
.
.
}
我已经创建了一个模型来访问getTuple并返回数据,这就是模型
public class BusinessModel implements Serializable {
@ColumnInfo(name="business_name")
public String businessName;
@ColumnInfo(name="business_address")
public String businessAddress;
@ColumnInfo(name="business_phone")
public String businessPhone;
@ColumnInfo(name="business_manager")
public String businessManager;
public String getBusinessName() {
if (businessName == null)
return "";
return businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
.
.
.
还有我的DatabaseAsyncUtil
public class GetBusinessModelAsyncTask extends AsyncTask<Void, Void,
List<BusinessModel>> {
private AppDatabase db;
private final DataBaseCallBack dataBaseCallBack;
public GetBusinessModelAsyncTask(AppDatabase userDatabase, DataBaseCallBack dataBaseCallBack) {
db = userDatabase;
this.dataBaseCallBack = dataBaseCallBack;
}
@Override
protected List<BusinessModel> doInBackground(Void... voids) {
List<BusinessModel> businessModel = db.businessDao().getTuple();
return businessModel;
}
@Override
protected void onPostExecute(List<BusinessModel> businessModelList) {
super.onPostExecute(businessModelList);
if (businessModelList != null) {
dataBaseCallBack.onSuccess(businessModelList, ApplicationConstants.SUCCESS_MSG);
} else
dataBaseCallBack.onFailure(ApplicationConstants.ERROR_CODE, ApplicationConstants.ERROR_MSG);
}
}
//kkk
class GetBusinessAsyncTask extends AsyncTask<Void, Void,
Business> {
private final DataBaseCallBack dataBaseCallBack;
private AppDatabase db;
GetBusinessAsyncTask(AppDatabase userDatabase, DataBaseCallBack dataBaseCallBack) {
db = userDatabase;
this.dataBaseCallBack = dataBaseCallBack;
}
@Override
protected Business doInBackground(Void... voids) {
Business business;
try {
business = db.businessDao().getAll();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return business;
}
@Override
protected void onPostExecute(Business business) {
super.onPostExecute(business);
if (business != null)
dataBaseCallBack.onSuccess(business, ApplicationConstants.SUCCESS_MSG_2_SIGN_IN);
else
dataBaseCallBack.onFailure(ApplicationConstants.ERROR_CODE, ApplicationConstants.ERROR_MSG_2);
}
}
我对Room db并不是很熟悉。我正在尝试通过在应用上实施来学习,而我一直被困在这里。