您好我使用领域将数据保存到relam数据库,但它没有将数据保存到数据库
在我的应用程序类
中 public void SaveUserData(final UserData userData, Realm realm)
{
final String userdata = new Gson().toJson(userData);
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
// realm.createObjectFromJson(com.mediquick.databaseModel.UserData.class, userdata);
com.mediquick.databaseModel.UserData user = realm.createObject(com.mediquick.databaseModel.UserData.class);
user.setLname(userData.getLname());
user.setEmail(userData.getEmail());
user.setToken(userData.getToken());
user.setAddress(userData.getAddress());
user.setMobile_no(userData.getMobile_no());
user.setDob(userData.getDob());
user.setName(userData.getName());
user.setProfile_pic(userData.getProfile_pic());
user.setUser_type(userData.getUser_type());
}
});
在我的保存活动中
public String getUserToken(Realm realm)
{
final com.mediquick.databaseModel.UserData user = realm.where(com.mediquick.databaseModel.UserData.class).findFirst();
return (user!=null)?user.getToken():null;
}
当我检索一个值
deploy.yml
当我从数据库中检索令牌时,它会返回空值
任何想法..
答案 0 :(得分:0)
你可以尝试这个保存功能:
// SAY YES TO THIS
Realm realm = null;
try { // I could use try-with-resources here
realm = Realm.getDefaultInstance();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.insertOrUpdate(dog);
}
});
} finally {
if(realm != null) {
realm.close();
}
}
在您的代码中,您缺少电话
realm.insertOrUpdate(user)
在executeTransaction
函数内。
答案 1 :(得分:0)
在onCreate函数之前声明Realm对象:
Realm realmDB;
现在在onCreate方法中写下这些行:
Realm.init(this);
realmDB=Realm.getDefaultInstance();
在保存按钮上单击方法:
try{
realmDB.beginTransaction();
UserSchedule us = realmDB.createObject(UserSchedule.class);
us.setId(userData.get_ID());
us.setDate(userData.getDate());
realmDB.commitTransaction();
}
catch (Exception ex){
Log.d("RError",ex.toString());
Toast.makeText(this, "Error in realm", Toast.LENGTH_SHORT).show();
}
获取记录:
RealmResults<UserSchedule>userSchedules=realmDB.where(UserSchedule.class).findAll();
for (UserSchedule userSchedule:userSchedules)
{
Toast.makeText(this,userSchedule.getId(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, userSchedule.getId(), Toast.LENGTH_SHORT).show();
}
删除记录:
RealmResults<UserSchedule> result = realmDB.where(UserSchedule.class).findAll();
UserSchedule userSchedule = result.where().equalTo("Id", getItem(position).getS_ID()).equalTo("Date",getItem(position).getDate()).findFirst();
if(userSchedule!=null)
{
if (!realmDB.isInTransaction())
{
realmDB.beginTransaction();
}
userSchedule.deleteFromRealm();
realmDB.commitTransaction();
}
else
{
Toast.makeText(this,"No Record.", Toast.LENGTH_SHORT).show();
}
答案 2 :(得分:0)
public void saveData(){
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
Students students = bgRealm.createObject(Students.class);
students.setName(name.getText().toString().trim());
students.setCity(city.getText().toString().trim());
students.setDegree(degree.getText().toString().trim());
students.setGender(gender.getSelectedItem().toString().trim());
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
// Transaction was a success.
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
// Transaction failed and was automatically canceled.
}
});
}