How to check if data is inserted in Room Database

后端 未结 7 2549
执念已碎
执念已碎 2021-02-19 22:07

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

相关标签:
7条回答
  • 2021-02-19 22:19

    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

    0 讨论(0)
  • 2021-02-19 22:21

    You can use Stetho to see your DB and Shared pref file in chrome dev tool

    http://facebook.github.io/stetho/

    0 讨论(0)
  • 2021-02-19 22:25

    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:

    1. 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'
      
    2. Start the debug build of your app

    3. 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
      
    4. 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.

    5. 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.

    0 讨论(0)
  • 2021-02-19 22:25

    One of the easiest way is:

    1. First download DB Navigator as plugin in Android Studio(For more visit: https://plugins.jetbrains.com/plugin/1800-database-navigator).

    2. Go to view> Tools Window> Device File explorer and download the instance of your DB.

    3. 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.

    4. For more info please refer to the link provided above.

    0 讨论(0)
  • 2021-02-19 22:27

    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

    0 讨论(0)
  • 2021-02-19 22:32

    There are multiple ways you can test that.

    1. As @Ege Kuzubasioglu mentioned you can use stetho to check manually (Need minor change to code).
    2. 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.

    3. 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));
    }
    
    0 讨论(0)
提交回复
热议问题