1

我正在尝试使用 Room 数据库制作应用程序。我有一张鸟桌和一张家庭桌。(一对多关系)。

@Entity(tableName ="bird",
        foreignKeys =
        @ForeignKey(entity = Family.class,
        parentColumns = "id",
        childColumns = "familyId",
        onDelete = CASCADE),
        indices = {@Index("familyId")})


public class Bird {

    @PrimaryKey(autoGenerate = true)
    private int id;
    private int familyId;
    private String name;
    private String family;
    private String description;
    private String biology;

    public Bird(@NonNull String name, @NonNull String family, String description, String biology){
        this.name = name;
        this.family = family;
        this.description= description;
        this.biology = biology;
        this.familyId = familyId;
    }
@Entity(tableName = "family")
public class Family {

    @PrimaryKey(autoGenerate = true)
    private int id;
    private String family;

    public Family(@NonNull String family)
    {
        this.family = family;
    }

每次我尝试运行我的鸟类列表(在实现家庭实体之前运行良好)时,什么都没有出现,当我尝试使用 @Insert 查询添加一只新鸟时,我被这个打击了:

Caused by: android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY)
        at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
        at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:879)
        at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:790)
        at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:88)
        at androidx.sqlite.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:51)
        at androidx.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:64)
        at com.example.room.Dao_Impl.insertBird(Dao_Impl.java:115)
        at com.example.room.BirdRepository$InsertBirdAsyncTask.doInBackground(BirdRepository.java:56)
        at com.example.room.BirdRepository$InsertBirdAsyncTask.doInBackground(BirdRepository.java:45)
        at android.os.AsyncTask$3.call(AsyncTask.java:378)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289) 
        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:919)

你们有什么想法可以从哪里来吗?

我也是这样填写数据库的吗?

private static void fillWithSartingData(Context context){
    Dao dao = getInstance(context).dao();
    FamilyDao familyDao = getInstance(context).familyDao();

    JSONArray birds = loadJSONArray(context);
    JSONArray families = loadJSONArray(context);

    try{

        for(int i = 0; i< families.length(); i++){
            JSONObject familyObj = families.getJSONObject(i);

            String family = familyObj.getString("FamilieF");
            familyDao.insertFamily(new Family(family));
        }


        for(int i = 0; i< birds.length(); i++){
            JSONObject bird = birds.getJSONObject(i);
            String engName = bird.getString("NameEng");
            String family = bird.getString("FamilieF");
            String description = bird.getString("Description");
            String biology = bird.getString("Biologie");

            dao.insertBird(new Bird(engName, family, description, biology));

        }

    } catch (JSONException e){

    }
}

谢谢!亚历克斯

4

1 回答 1

0

您违反了外键约束“FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY)”,因为您没有familyId在 Bird 类中设置

你必须familyIdBird插入鸟之前设置类

为此,familyId向bird 构造函数添加一个参数,如下所示:

public Bird(@NonNull String name, @NonNull String family, String description, String biology, long familyId){
        this.name = name;
        this.family = family;
        this.description= description;
        this.biology = biology;
        this.familyId = familyId;
    }

insertFamilyinfamilyDao应该返回 Long (如果它有一个 Family 参数)或 Long Array (如果它有 vararg Family 参数)然后你得到插入家庭的 idinsertFamily并像这样使用它familyId

for(int i = 0; i< families.length(); i++){
       JSONObject familyObj = families.getJSONObject(i);
       String family = familyObj.getString("FamilieF");
       long familyId = familyDao.insertFamily(new Family(family));// get familyId


       for(int i = 0; i< birds.length(); i++){
            JSONObject bird = birds.getJSONObject(i);
            String engName = bird.getString("NameEng");
            String family = bird.getString("FamilieF");
            String description = bird.getString("Description");
            String biology = bird.getString("Biologie");
            dao.insertBird(new Bird(engName, family, description, biology, familyId));// use familyId
        }
于 2020-03-24T22:05:33.633 回答