Android Room - Get the id of new inserted row with auto-generate

前端 未结 7 1313
野性不改
野性不改 2020-11-29 00:03

This is how I am inserting data into database using Room Persistence Library:

Entity:

@Entity
class User {
    @PrimaryKey(autoGenerate = true)
    p         


        
7条回答
  •  甜味超标
    2020-11-29 00:36

    According to the documentation functions annoted with @Insert can return the rowId.

    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 problem I have with this is that it returns the rowId and not the id and I still haven't found out how to get the id using the rowId.

    Sadly I can't comment yet, because I don't have 50 reputation, so I am posting this as an answer instead.

    Edit: I now know how to get the id from the rowId. Here is the SQL command:

    SELECT id FROM table_name WHERE rowid = :rowId
    

提交回复
热议问题