How To Check If Data Is Inserted In Room Database
Solution 1:
The problem was in the thread. Room doesn't allow you to run database queries in main thread. The call to insert method was inside of a try-catch block, and I was ignoring the exception. I fixed it, and here's how it reads right now.
doAsync {
val addedID = appContext.db.rallyDAO().addVehicleListItem(vehicle)
Logger.d("vehicle_lsit_item","Inserted ID $addedID")
}
Also, I reviewed the documentation, the insert method may return a Long (or List of Long in case of List passed to insert). I also changed the signature of insert, and here is the modified code
@Insert(onConflict = OnConflictStrategy.REPLACE)funaddVehicleListItem(vehicleListItem:VehicleListItem):Long
P.S. I am using anko for doAsync
Solution 2:
You can use Stetho to see your DB and Shared pref file in chrome dev tool
http://facebook.github.io/stetho/
EDIT: AS has a built-in DB tool now
Solution 3:
An option you may consider, if you have not access permission issues, is to navigate directly into your sqlite instance of the device and query the tables directly there.
If you use the emulator intehgrated with Android Studio or a rooted phone you can do the following:
adb root
adb remount
cd data/data/path/of/your/application/database
sqlite3 mydb.db
Then you can query your tables
Solution 4:
There are multiple ways you can test that.
- As @Ege Kuzubasioglu mentioned you can use stetho to check manually (Need minor change to code).
Pull database file from "data/data/yourpackage/databases/yourdatabase.db" to your local machine and use any applications to read the content inside the database. I personally use https://sqlitebrowser.org/. Pulling database file can be done either using the shell commands or use "Device File Explorer" from android studio.
Write TestCase to see if it is working. Here is an example of test case from one of my projects.
// Code from my DAO class
@Insert(onConflict = OnConflictStrategy.REPLACE) public abstract Long[] insertPurchaseHistory(List MusicOrders);
//My Test Case @Test public void insertPurchaseHistoryTest() {
// Read test data from "api-responses/music-purchase-history-response.json"InputStreaminputStream= getClass().getClassLoader()
.getResourceAsStream("api-responses/music-purchase-history-response.json");
// Write utility method to convert your stream to a string.StringtestData= FileManager.readFileFromStream(inputStream);
// Convert test data to "musicOrdersResponse"MusicOrdersResponsemusicOrdersResponse=newGson().fromJson(testData,MusicOrdersResponse.class);
// Insert inmateMusicOrders and get the list of
Long[] rowsInserted = tracksDao.insertPurchaseHistory(musicOrdersResponse.getmusicOrders());
assertThat(rowsInserted.length,Matchers.greaterThan(0));
}
Solution 5:
There are two cases regarding insertion.
Dao methods annotated with @Insert returns:
In case the input is only one number it returns a Long, check whether this Long equals One, if true then the Item was added successfully.
@Insertsuspendfuninsert( student: Student): Long
In case the input is varargs it returns a LongArray, check whether the size of varargs equals the LongArray size, if true then items were added successfully
@Insertsuspendfuninsert(vararg students: Student): LongArray
Post a Comment for "How To Check If Data Is Inserted In Room Database"