gpt4 book ai didi

android - 从 Room DB 返回项目计数

转载 作者:行者123 更新时间:2023-11-29 23:04:38 25 4
gpt4 key购买 nike

如果父项具有通过外键关联的子项,我试图阻止从 Room DB 中删除父项。

我正在研究学位跟踪器。如果某个学期有类(class),则无法删除该学期。如果该学期没有类(class),则可以删除该学期。我正在尝试获取具有相关术语 ID 的类(class)数量,并使用简单的 if 语句在没有类(class)的情况下删除该术语,如果该术语有类(class)则使用弹出式警报并指示用户删除类(class)在删除术语之前。

来自 TermEditorActivity.java

switch(item.getItemId()){
...
case R.id.delete_term:

int coursecount = queryCourses(termIdSelected);

if(coursecount > 0){
AlertDialog.Builder a_builder = new
AlertDialog.Builder(TermEditorActivity.this);
a_builder.setMessage("Courses are assigned for this
term!\n\nYou must remove all courses" +
"prior to deleting this term.")
.setCancelable(false)
.setPositiveButton("Okay", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {

finish();
}
});
AlertDialog deleteAllAlert = a_builder.create();
deleteAllAlert.setTitle("CANNOT DELETE TERM!!!");
deleteAllAlert.show();
return true;
}else{
mViewModel.deleteTerm();
startActivity(new Intent(TermEditorActivity.this,
MainActivity.class));
}
...
public int queryCourses(int term) {
int course = mViewModel.queryCourses(term);
return course;
}

来自 ViewModel:

public int queryCourses(final int term) {
int course = mRepository.queryCourses(term);
return course;
}

来自 AppRepository(这是我认为事情崩溃的地方):

public int queryCourses(final int term) {
// executor.execute(new Runnable() {
// @Override
// public void run() {
return count = courseDb.termDao().queryCourses(term);
// }
// });
// return count;
// }

or with threading:

public int queryCourses(final int term) {
executor.execute(new Runnable() {
@Override
public void run() {
count = courseDb.termDao().queryCourses(term);
}
});
return count;
}

来自 TermDAO:

@Query("SELECT COUNT(*) FROM course WHERE term_id = :termIdSelected")
int queryCourses(int termIdSelected);

这会导致运行时错误,当按下删除按钮时它会崩溃。这个概念很简单 - 使用术语的 ID 来查询类(class)数据库,以获取具有术语 ID 外键的类(class)数。如果没有,则删除该术语并返回到术语列表。如果有类(class)(计数 > 0)提醒用户并完成而不删除。

没有线程的异常:

java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.

当使用线程时,它会删除有或没有类(class)的术语,并且当有类(class)附加到该术语时不会出现警告。在 Debug模式下运行,当有一门类(class)时 coursecount 返回 0,因此查询运行不正常。

我需要做些什么才能从线程中获取值吗?

这是为 RESTRICT 抛出 SQLiteConstraintException 时运行时错误的崩溃日志约束。即使使用 Exception 也不会被捕获。

E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
Process: com.mattspriggs.termtest, PID: 23927
android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 1811 SQLITE_CONSTRAINT_TRIGGER)
at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:784)
at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeUpdateDelete(FrameworkSQLiteStatement.java:45)
at android.arch.persistence.room.EntityDeletionOrUpdateAdapter.handle(EntityDeletionOrUpdateAdapter.java:70)
at com.mattspriggs.termtest.database.TermDao_Impl.deleteTerm(TermDao_Impl.java:144)
at com.mattspriggs.termtest.database.AppRepository$4.run(AppRepository.java:83)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)

最佳答案

Room 实际上支持这种行为:

当为你的子实体定义foreign 键时,你只需要将 Action onDelete设置为RESTRICT,而父实体有子实体相关无法删除。

您的子类应该如下所示:

@Entity(tableName = "child_table",foreignKeys ={
@ForeignKey(onDelete = RESTRICT,entity = ParentEntity.class,
parentColumns = "uid",childColumns = "parentId")},
indices = {
@Index("parentId"),
})
public class ChildEntity {
@PrimaryKey
public String id;
public String parentId;
}

你的父类看起来像这样:

@Entity
public class ParentEntity{
@PrimaryKey
public String uid;
}

可以查看here有关如何定义外键的更多信息

关于android - 从 Room DB 返回项目计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56693432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com