我试图防止从 Room DB 中删除父级,如果它有通过外键关联的子级。
我正在研究学位跟踪器。如果一个学期有课程,则不能删除该学期。如果该学期没有课程,则可以删除该学期。我正在尝试获取具有相关术语 ID 的课程计数,并使用一个简单的 if 语句来删除该术语(如果它没有课程)并使用弹出警报(如果该术语有课程)并指示用户删除课程在删除该术语之前。
来自 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;
}
从视图模型:
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 来查询课程数据库以获取具有术语 id 外键的课程数。如果没有,请删除该术语并返回到术语列表。如果有课程(计数> 0)提醒用户并完成而不删除。
没有线程的异常:
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
使用线程时,它会删除包含或不包含课程的术语,并且当术语附加课程时不会出现警报。在调试模式下运行,当只有一门课程时,coursecount 返回 0,因此查询运行不正常。
我需要做些什么来从线程中获取价值吗?
这是针对RESTRICT约束引发 SQLiteConstraintException 时的运行时错误的崩溃日志。即使使用异常也不会被捕获。
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)