gpt4 book ai didi

java - 具有一对一关系外键约束的房间数据库失败错误

转载 作者:行者123 更新时间:2023-12-02 10:21:44 25 4
gpt4 key购买 nike

我有一个包含两个表“学生”和“类(class)”的数据库。我可以在学生表中插入数据,但是当我尝试将数据插入类(class)表时应用程序崩溃。这是日志猫:

2019-01-21 19:56:22.302 28939-29121/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.rodroiddev.catalogulinstructoruluiauto, PID: 28939
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:50)
at android.arch.persistence.room.EntityInsertionAdapter.insertAndReturnId(EntityInsertionAdapter.java:114)
at com.rodroiddev.catalogulinstructoruluiauto.db.CourseDao_Impl.insert(CourseDao_Impl.java:104)
at com.rodroiddev.catalogulinstructoruluiauto.course.CourseRepository$InsertCourseAsyncTask.doInBackground(CourseRepository.java:53)
at com.rodroiddev.catalogulinstructoruluiauto.course.CourseRepository$InsertCourseAsyncTask.doInBackground(CourseRepository.java:44)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
at java.lang.Thread.run(Thread.java:764) 

这里是插入代码:

private static class InsertCourseAsyncTask extends AsyncTask<Course, Void, Void> {
private CourseDao courseDao;

public InsertCourseAsyncTask(CourseDao courseDao) {
this.courseDao = courseDao;
}

@Override
protected Void doInBackground(Course... courses) {
courseDao.insert(courses[0]);
return null;
}
}

类(class).java

@Entity(tableName = "course_table",
foreignKeys = @ForeignKey(entity = Student.class,
parentColumns = "sId",
childColumns = "studentId",
onDelete = CASCADE),
indices = {@Index("studentId")})

公开课类(class){

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "cId")
public int id;

public String kmStart;

public String kmStop;

@ColumnInfo(name = "studentId")
public int studentId;

public Course(String kmStart, String kmStop) {

this.kmStart = kmStart;
this.kmStop = kmStop;

}

public void setId(int id) {
this.id = id;
}

public int getId() {
return id;
}

public String getKmStart() {
return kmStart;
}

public String getKmStop() {
return kmStop;
}

public void setStudentId(int studentId) {
this.studentId = studentId;
}

public int getStudentId() {
return studentId;
}

}

CourseDao

@Dao

公共(public)接口(interface)CourseDao{

@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(Course... courses);

@Update(onConflict = OnConflictStrategy.IGNORE)
void update(Course course);

@Delete
void delete(Course course);

@Query("DELETE FROM course_table")
void deleteAllCourses();

@Query("SELECT * FROM course_table ORDER BY kmStart ASC")
LiveData<List<Course>> getAllCourses();

}

我希望您能根据我在这里发布的信息帮助我。谢谢!

最佳答案

保存前是否将studentId设置为类(class)对象?如果你不这样做,它会崩溃,因为它会尝试为studentId添加0作为不存在的外键值。

将Course对象的studentId设置为Student对象的id。并先保存Student,这样它就可以获得一个id

关于java - 具有一对一关系外键约束的房间数据库失败错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54295883/

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