因此,我有一个用于会议的模拟数据库,其中正在为作者,论文,审稿人等创建表格。

审稿人提供了一封电子邮件,该电子邮件是指计划委员会的电子邮件。这是我施加的关键约束。然后,我将数据添加到PC表中,然后尝试将数据添加到审阅者表中。这是我得到的错误:

Exception encountered com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`sampledb`.`review`, CONSTRAINT `review_ibfk_2` FOREIGN KEY (`email`) REFERENCES `pcmember` (`email`))


以下是其他功能:

public int loadPCMember(){
    String tablename = "pcmember";
    String create = "CREATE TABLE IF NOT EXISTS pcmember(email VARCHAR(100), name VARCHAR(50), PRIMARY KEY (email));";
    makeTable(create);
    System.out.println("made table pcmember");
    //CSV Reader
    String[][] content = CSVReader(tablename,2);

    for(int i = 0 ; i < content.length; i++){
        try{
            String query = "INSERT INTO pcmember(email,name) VALUES (?,?)";
            PreparedStatement ps2 = net.prepareStatement(query);
            ps2.setString(1, content[i][0]);
            ps2.setString(2, content[i][1]);
            ps2.executeUpdate();
            System.out.println((i+1)+ " done");
        // Throw exception
        }catch (SQLException e){System.out.println("Exception encountered");return 0;}
    }
    System.out.println("PC Member Done");
    return 1;
}
//Load next
public int loadReview(){
    String tablename = "review";
    String create = "CREATE TABLE IF NOT EXISTS review(reportid INTEGER, sdate DATE, comment VARCHAR(250), recommendation VARCHAR(6), paperid INTEGER NOT NULL, email VARCHAR(100) NOT NULL, PRIMARY KEY(reportid), FOREIGN KEY (paperid) REFERENCES paper(paperid), FOREIGN KEY(email) REFERENCES pcmember(email));";
    makeTable(create);
    System.out.println("made table review");
    //CSV Reader
    String[][] content = CSVReader(tablename,6);

    for(int i = 0 ; i < content.length; i++){
        System.out.println("" + content[i][0] + "\t" +content[i][1] + "\t" + content[i][2] + "\t" +content[i][3] + "\t" +content[i][4] + "\t" +content[i][5]);
        try{
            //SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
            //java.util.Date date = sdf.parse(content[i][1]);
            //System.out.println(""+date);
            //Date newDate = new Date(date.getTime());
            //System.out.println(""+newDate);
            String query = "INSERT INTO review(reportid,sdate,comment,recommendation,paperid,email) VALUES (?,?,?,?,?,?)";
            PreparedStatement ps2 = net.prepareStatement(query);
            ps2.setInt(1, Integer.parseInt(content[i][0]));
            ps2.setDate(2, java.sql.Date.valueOf(content[i][1]));
            ps2.setString(3, content[i][2]);
            ps2.setString(4, content[i][3]);
            ps2.setInt(5, Integer.parseInt(content[i][4]));
            ps2.setString(6, content[i][5]);
            ps2.executeUpdate();
            System.out.println((i+1)+ " done");
        // Throw exception
        }catch (SQLException e){System.out.println("Exception encountered "+ e);return 0;
        }//catch (ParseException e){System.out.println("Parse Exception encountered "+e);}

    }
    System.out.println("Review Done");
    return 1;
}


我对关键约束有很好的理解,而且对于具有相同确切电子邮件的CSV文件,我也很满意,那么可能导致此错误的原因是什么?

最佳答案

我是SQL专家,不是Java专家。如果我提出任何建议或建议,因为这些没有任何意义-你知道为什么。需要明确的是-根据该错误消息和您所包含的sql,您似乎试图将一条记录插入审阅表中,而该记录的pcmember表中没有该电子邮件地址,因此违反了查看表格。这似乎是您使用的CVS的数据问题。但是,既然您提到您对数据文件充满信心,是否有可能在pcmember INSERT成功完成之前尝试将行插入检查表中?

08-24 17:55