[英]Room Database - How to return entire row after insert?
I have query that inserts a row我有插入一行的查询
@Dao
public interface NoteDao {
@Insert
Long insert(Note note);
}
I'm using RxJava to perform the query on the background thread:我正在使用 RxJava 在后台线程上执行查询:
public void insert(Note note) {
Single.fromCallable(() -> noteDao.insert(note))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<Long>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onSuccess(@NonNull Long aLong) {
Log.d(TAG, "onSuccess: New row Id: " + aLong);
}
@Override
public void onError(@NonNull Throwable e) {
}
});
}
Currently it successfully returns, the newly created primary key for the inserted DAO.目前它成功返回,为插入的 DAO 新创建的主键。 How would I return the entire inserted row, WITHOUT performing a second query using the new row id?我将如何返回整个插入的行,而不使用新的行 ID 执行第二个查询?
In postgresql I would do something like this:在 postgresql 我会做这样的事情:
`INSERT INTO note_table(note_title, note_description) VALUES ($1, $2) RETURNING *`
Not sure how to do it with the Room library不确定如何使用 Room 库
As stated in the documents of Transcation , if you want to perform two queries in one shot you have to use transactions and there is no other option as far as I know for standard database operations如Transcation文档中所述,如果您想一次执行两个查询,则必须使用事务,据我所知,对于标准数据库操作,没有其他选择
check below as here we are doing you should do similar在下面检查,因为我们在这里做你应该做类似的事情
@Dao
public interface NoteDao {
@Insert
Long insert(Note note);
@@Query(“SELECT * FROM Note WHERE noteId = :id)
Long getNote(id Long);
@Transaction
public void insertAndRetrieve(Note note):Note {
// Anything inside this method runs in a single transaction.
val id = insert(note);
return getNote(id);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.