DAO class does not recognize the table name and id

Viewed 213

I'm trying to save my images using room database.

Saving is not working. I guess my DAO class does not recognize the table name and id. Can you review the following pictures and help me to solve this issue?

@Entity(tableName = "my_images") 

public class MyImagesEntity {
    

    @PrimaryKey(autoGenerate = true) 
    public int image_id;
    public String image_title;
    public String image_description;
    //images/sounds are stored in database as binary large object
    public byte[] image;

    //constructor


    public MyImagesEntity(String image_title, String image_description, byte[] image) {
        this.image_title = image_title;
        this.image_description = image_description;
        this.image = image;
    }
}

The @Dao annotated interface

@Dao
public interface MyImagesDao {


    @Insert
    void insert(MyImagesEntity myImagesEntity);

    @Delete
    void delete(MyImagesEntity myImagesEntity);

    @Update
    void update(MyImagesEntity myImagesEntity);

    @Query("SELECT * FROM my_images ORDER BY image_id ASC")
    LiveData<List<MyImagesEntity>> getAllImages();

}

The dependencies

     def room_version = "2.4.2"

implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

def lifecycle_version = "2.5.0-alpha03"

// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"

// Annotation processor
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
1 Answers

Saving is not working.

Using the code you have supplied, I don't believe that your issue is that data is not being saved as using the following additional code :-

@Database(entities = {Data.class/*<<<<< due to test using a hijacked project*/,MyImagesEntity.class}, exportSchema = false, version = 1)
abstract class AppDatabase extends RoomDatabase {
   abstract DataDao getDataDao(); //<<<<< due to test using a hijacked project
   abstract MyImagesDao getMyImagesDao();

   private static volatile AppDatabase instance = null;

   static AppDatabase getAppDatabase(Context context) {
      if (instance == null) {
         instance = Room.databaseBuilder(context, AppDatabase.class,"the_database.db")
                 .allowMainThreadQueries()
                 .build();
      }
      return instance;
   }
}

and the following in an activity:-

    db = AppDatabase.getAppDatabase(this);
    myImagesDao = db.getMyImagesDao();

    myImagesDao.insert(new MyImagesEntity("Image001","Image 1 description",new byte[]{10,11,12,13,14,15,16,17}));
    myImagesDao.insert(new MyImagesEntity("Image002","Image 2 description",new byte[]{0,1,2,3,4,5,6,7,8,9,}));
    for(MyImagesEntity mie: myImagesDao.getAllImagesOther()) {
        Log.d("DBINFO","Image is " + mie.image_description + " Description is " + mie.image_description);
    }

with getAllImagesOther being (without LiveData) :-

@Query("SELECT * FROM my_images ")
List<MyImagesEntity> getAllImagesOther();

Then the result is :-

D/DBINFO: Image is Image 1 description Description is Image 1 description
D/DBINFO: Image is Image 2 description Description is Image 2 description

and using App Inspection you can see that the data has been added :-

enter image description here

Therefore I believe that you issues is with how your are observing (or not) the LiveData. I'd suggest using App Inspection to see if the data is being saved.

Related