我正在尝试使用Android的SQLite包装器Room在特定ID处插入一行。
[基本上,用例是我从服务器获取一行数据,并且我希望将远程行的ID用作Room中本地存储行的主键。
代码:
@Entity
class UserModel {
@PrimaryKey
public int id;
public String firstName;
public String lastName;
}
@Dao
public interface UserDAO {
/**
* Select all Users from the User table.
*
* @return all users.
*/
@Query("SELECT * FROM " + UserRoomDB.TABLE_NAME)
UserModel getUser();
/**
* Insert a user in the database. If the user already exists, replace it.
*
* @param user the user to be inserted.
*/
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertUser(UserModel user);
/**
* Update a user.
*
* @param user task to be updated
* @return the number of users updated. This should always be 1.
*/
@Update
int updateUser(UserModel user);
}
private void cacheUser(UserModel userModel){
UserRoomDB db = Room.databaseBuilder(this.appContext,
UserRoomDB.class, UserRoomDB.DB_NAME).build();
db.userDAO().insertUser(userModel);
}
据我所能在您的代码中看到的,您正在获取表名而不是ID本身的用户