I am using Room Database from the new Architecture components in my project. I am adding some data through dao, but when trying to retrieve it I am not getting it. Can you pleas
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)
fun addVehicleListItem(vehicleListItem:VehicleListItem):Long
P.S. I am using anko for doAsync
You can use Stetho to see your DB and Shared pref file in chrome dev tool
http://facebook.github.io/stetho/
You can use Android Debug Database to access your application's databases via a web browser. Android Debug Database exposes the databases via an embedded web server.
Use it as follows:
Include it as debugImplementation
dependency in your app's build.gradle
so that it will only be included in debug build and not in release build:
debugImplementation 'com.amitshekhar.android:debug-db:1.0.3'
Start the debug build of your app
The embedded web server launches automatically and announces its address and port in the logs:
D/DebugDB: Open http://XXX.XXX.X.XXX:8080 in your browser
If you are running the app over USB, setup portforwarding:
adb forward tcp:8080 tcp:8080
If you are not running the app over USB, your Android phone and workstation need to be in the same network and your workstation should be able to ping the Android phone.
Finally, open the link from the logs in the browser.
Note that it works just fine without rooting the device as the web server runs inside your app context.
One of the easiest way is:
First download DB Navigator as plugin in Android Studio(For more visit: https://plugins.jetbrains.com/plugin/1800-database-navigator).
Go to view> Tools Window> Device File explorer and download the instance of your DB.
Open DB Browser(From left Pane window) and browse your DB instance you have downloaded. Setup the connection and you are good to browser you database.
For more info please refer to the link provided above.
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
There are multiple ways you can test that.
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"
InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream("api-responses/music-purchase-history-response.json");
// Write utility method to convert your stream to a string.
String testData = FileManager.readFileFromStream(inputStream);
// Convert test data to "musicOrdersResponse"
MusicOrdersResponse musicOrdersResponse = new Gson().fromJson(testData,MusicOrdersResponse.class);
// Insert inmateMusicOrders and get the list of
Long[] rowsInserted = tracksDao.insertPurchaseHistory(musicOrdersResponse.getmusicOrders());
assertThat(rowsInserted.length,Matchers.greaterThan(0));
}