java 无法添加或更新子行:外键约束失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6126514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:26:18  来源:igfitidea点击:

Cannot add or update a child row: a foreign key constraint fails

javamysqljdbc

提问by Mike

Anybody know what's wrong with my code? I am using a simple INSERT statement (as opposed to the regular PreparedStatement because I am trying to utilize mySQL's AES_ENCRYPT/DECRYPT functions. I do not know how to use them with the regular set up of the PreparedStatements. with all the funky question marks.

有人知道我的代码有什么问题吗?我正在使用一个简单的 INSERT 语句(与常规 PreparedStatement 相反,因为我试图利用 mySQL 的 AES_ENCRYPT/DECRYPT 函数。我不知道如何将它们与 PreparedStatements 的常规设置一起使用。带有所有时髦的问号。

I keep getting the classic:

我不断得到经典:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (video_game_db.login, CONSTRAINT login_ibfk_1FOREIGN KEY (cust_ID) REFERENCES customer(cust_ID))

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(video_game_db. login,CONSTRAINT login_ibfk_1FOREIGN KEY(cust_ID)REFERENCES customercust_ID))

Of course, it's driving me batty.

当然,这让我很生气。

I spent all night trying to make the regular JDBC implementation of the cipher "stuff" work. In the end, I could only encrypt the strings, but could not decrypt them. I figured it had to do something with mySQL as opposed to JDBC, as before I input them into the database they decrypted fine, but afterwards I could get it to work.

我花了一整夜试图使密码“东西”的常规 JDBC 实现工作。最后,我只能加密字符串,但无法解密它们。我认为它必须对 mySQL 做一些事情而不是 JDBC,因为在我将它们输入到数据库之前,它们解密得很好,但之后我可以让它工作。



Please take a look at my JDBC table DDL and DML statements below:

请在下面查看我的 JDBC 表 DDL 和 DML 语句:

DDL

数据线

Statement s = conn.createStatement();
s.executeUpdate("CREATE TABLE customer("+"cust_ID CHAR(10) NOT NULL,"+"PRIMARY KEY(cust_ID),"+"first_Name CHAR(30)NOT NULL,mI CHAR(2),last_Name CHAR(50)NOT NULL,street_Name CHAR(50),city CHAR(30) NOT NULL,state CHAR(50) NOT NULL,"
            +"zip_Code CHAR(5) NOT NULL, home_Phone CHAR(12) UNIQUE, referrer CHAR(30), quantity INTEGER NOT NULL, item_No CHAR(10))"); 
s.executeUpdate("CREATE TABLE login ("+"user_Name CHAR(50) NOT NULL,"+"PRIMARY KEY(user_Name),"+"pass_Word CHAR(50)NOT NULL, cust_ID CHAR(10))");  

DML

数据管理语言

public static void setCustTable(String cust_ID, String lName, String fName, String mI, String street_Name, String city, String state,
  String zip_Code, String home_Phone, String referrer, int quantity, String itemNo)throws IOException, SQLException 
{

// connect to database
try
{    

    PreparedStatement stat = conn.prepareStatement("INSERT INTO customer (cust_ID, first_Name, mI, last_Name, street_Name, city, state, zip_Code, home_Phone, referrer, quantity, item_No)"
                + " VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");

    stat.setString(1, cust_ID);
    stat.setString(2, fName);
    stat.setString(3, mI);
    stat.setString(4, lName);
    stat.setString(5, street_Name);
    stat.setString(6, city);
    stat.setString(7, state);
    stat.setString(8, zip_Code);
    stat.setString(9, home_Phone);
    stat.setString(10, referrer);
    stat.setInt(11, quantity);
    stat.setString(12, itemNo);

    stat.executeUpdate();
}  

 catch (SQLException e) 
{
    e.printStackTrace();
    throw e;
}

**********************************************************************************



public static void insertLoginData(String username5, String password5, String custID5)throws IOException, SQLException, NoSuchAlgorithmException, InvalidKeyException

{
    Statement s = conn.createStatement();
    String insert="INSERT INTO login VALUES('username5', AES_ENCRYPT('text','password5'),'custID5')";
    s.executeUpdate(insert);

采纳答案by Alex Gitelman

For what it worth, I don't see any insert into customer. Error message tells that there is a foreign key constraint from login to customer. So customer entry must exist prior to login insert.

对于它的价值,我没有看到任何插入到customer. 错误消息表明从登录到客户存在外键约束。所以客户条目必须在登录插入之前存在。



Was there intention to quote 'username5'? I think it is a parameter to method but insert uses literal.

是否有意引用“username5”?我认为它是方法的参数,但插入使用文字。