Reputation: 562
After Insert data, checking the data at Database Inspector of Android Studio. But the data isn't in DB. Also, no error at all.
Table and table column are looks OK, but no data at the table even refresh.
I want to write it to DB. How to debug it further?
In Fragment onCreateView,
View rview = inflater.inflate(R.layout.fragment_db, container, false);
context = rview.getContext();
vanDB = VanDB.getInstance(context);
String sDate = "21/8/2022";
Date a_date = null;
try {
a_date = new SimpleDateFormat("dd/MM/yyyy").parse(sDate);
} catch (ParseException e) {
e.printStackTrace();
}
Van a_van = new Van("123", a_date, false);
vanDB.vanDao().insertVan(a_van);
VanDB
@Database(entities = {Van.class}, version = 1, exportSchema = false)
public abstract class VanDB extends RoomDatabase {
public abstract VanDao vanDao();
public static VanDB van_db;
public static VanDB getInstance(Context context) {
if (null == van_db) {
van_db = buildDatabaseInstance(context);
}
return van_db;
}
private static VanDB buildDatabaseInstance(Context context) {
return Room.databaseBuilder(context, VanDB.class, "vans.db").allowMainThreadQueries().build();
}
public void cleanUp(){
van_db = null;
}
}
Van
@Entity(indices = {@Index(value = {"serial"}, unique = true)}, tableName = 'van')
public class Van {
@PrimaryKey(autoGenerate = false)
@NonNull
private String serial;
@NonNull
private Date expiry_date;
private boolean validity;
public Van(@NonNull String serial, @NonNull Date expiry_date, boolean validity) {
this.serial = serial;
this.expiry_date = expiry_date;
this.validity = validity;
}
// getter and setter...
VanDao
@Dao
public interface VanDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
Completable insertVan(Van van);
@Update(onConflict = OnConflictStrategy.REPLACE)
Completable updateVan(Van van);
}
Upvotes: 0
Views: 191
Reputation: 412
Stupid questions - your Van
class is have a public constructor for the Vest
class and it doesn't show the error?
Also, do use RxJava according to Completable
, instead of void
?
Upvotes: 1