2

I am inserting rows into Room database and I want to check if the row is inserted successfully.

This is my Dao

@Insert()
suspend fun insertOfflineData(offlineData: OfflineData): Long

@Entity
data class OfflineData(
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0,
    @ColumnInfo(name = "request_info_json")
    val requestInfoJson: String
)

When I insert a row the response is 1,2,3 and so on.. So it is the id that it is returning (is that right?)

So to check if the row is inserted correctly.

Can I just check that the inserted rowId is greater that 0 then it is successfully inserted into the DB.

private suspend fun insertOfflineData(offlineDataRequestInfo: String): Long {
        var result: Long = 0
        result = OfflineDatabaseManager.getInstance(app.applicationContext).insertOfflineData(
            OfflineData(
                0,
                offlineDataRequestInfo
            ))
        if(result > 0 ) {
            //SUCCEFFLULLY INSERTED
        } else {
            //UNSUCCESSFULL
        }
        return result
    }

Note: result I am getting is 1,2,3 etc which should be the rowId.

Please suggest if this approach is right? Basically if the insert is not successful I want to stop the user from proceeding further

Thank you for your suggestions and input R

BRDroid
  • 3,920
  • 8
  • 65
  • 143
  • AFAIK, when new row is added to db, Room will always return this row's primaryKey value i.e Long. If you have opted to `autoGenerate=true` room will generate it or else you have to specify it. – Mohammed Farhan Oct 23 '20 at 11:30
  • what I want to check is, if the result is > 0 that means room as successfully inserted the row and if not it is unsuccessful. ~Is this approach right in checking if my data is saved in the room succesfully ? – BRDroid Oct 23 '20 at 11:32
  • rowId <0 is always failed condition. – Mohammed Farhan Oct 23 '20 at 11:33
  • ok so you mean that will never happen? – BRDroid Oct 23 '20 at 11:34
  • Now I'm not sure what kind of failures you are expecting but you can specify a Conflict Strategy for inserts and if it's not conflict related I'm pretty sure an exception will be thrown. I don't think you can make assumptions based on the value of the rowId other than if you receive one the insert was successful – patrick.elmquist Oct 23 '20 at 11:34
  • then what would be the approach to check if the database insert operation hs failed (unsuccessful) please – BRDroid Oct 23 '20 at 11:34
  • how can I catch this exception please and where should I be including that code, can you please share an example – BRDroid Oct 23 '20 at 11:35
  • Yes, i never went into such condition where my row was not getting added. Even if you have any error in your table schema, Room will let you know while compiling the project itself i.e if you have query using wrong params. – Mohammed Farhan Oct 23 '20 at 11:35
  • But for conflicts, you can override it with Conflict stratergies. You should worry about this when you have opted for `autoGenerated=false` for primaryKey of any table. – Mohammed Farhan Oct 23 '20 at 11:37
  • I think I am not worried about conflicts for now, my main concern is to check is the row is inserted succesfully, if not inserted, how can i know that it error or how to catch that exception please – BRDroid Oct 23 '20 at 11:39

3 Answers3

5

According to the Docs here Room Dao @Insert

If the @Insert method receives only 1 parameter, it can return a long, which is the new rowId for the inserted item. If the parameter is an array or a collection, it should return long[] or List instead.

The bottom line is, A method annotated with the @Insert annotation can return:

  1. a long value for a single insert operation.
  2. a long[] or Long[] or List for multiple insert operations.
  3. void if you don't care about the inserted id(s).

If the return value is -1 or negative consider the operation is failed according to docs.

Edit Answering @patrick.elmquist comment

From Room Generated Code. EntityInsertionAdapter and SupportSQLiteStatement

/**
     * Execute this SQL statement and return the ID of the row inserted due to this call.
     * The SQL statement should be an INSERT for this to be a useful call.
     *
     * @return the row ID of the last row inserted, if this insert is successful. -1 otherwise.
     *
     * @throws android.database.SQLException If the SQL string is invalid for
     *         some reason
     */
    long executeInsert();
Jayanth
  • 5,954
  • 3
  • 21
  • 38
0

When inserting data to a sqlite database via a DAO , it emits a value. If that row is inserted successfully , it emits it's rowID (if it's a list of rows it emits array of rowID's)

If it's unsuccessful , it emits -1 (Long)

Supun Ayeshmantha
  • 499
  • 1
  • 5
  • 9
-1

You can use a database debug library like this one

ruben
  • 1,745
  • 5
  • 25
  • 47
  • thank you for responsing, for debugging this good, but my purpose of checking is not to debug but to stop the user from proceeding further in the app – BRDroid Oct 23 '20 at 11:33