Java Dom解析器";无法添加或更新子行:外键约束失败;
我有一个xml文件。我需要读取数据并将其插入mysql数据库。我有一个考试表,eno是这个表的主键和问题表中的外键。虽然问题表是空的,但它会抛出一个异常Java Dom解析器";无法添加或更新子行:外键约束失败;,java,mysql,dom,xml-parsing,Java,Mysql,Dom,Xml Parsing,我有一个xml文件。我需要读取数据并将其插入mysql数据库。我有一个考试表,eno是这个表的主键和问题表中的外键。虽然问题表是空的,但它会抛出一个异常 Cannot add or update a child row: a foreign key constraint fails (`a3_2`.`answeroption`, CONSTRAINT `answeroption_ibfk_1` FOREIGN KEY (`eno`, `qno`) REFERENCES `question` (`
Cannot add or update a child row: a foreign key constraint fails (`a3_2`.`answeroption`, CONSTRAINT `answeroption_ibfk_1` FOREIGN KEY (`eno`, `qno`) REFERENCES `question` (`eno`, `qno`))
那么,这段代码中有什么错误呢
这里是我的xml文件:
<Assignment3>
<exam>
<eno>3</eno>
<etitle>Elementary History</etitle>
<timeAllowed>10</timeAllowed>
<numberOfQuestionsPerPage>3</numberOfQuestionsPerPage>
</exam>
<question>
<eno>3</eno>
<qno>1</qno>
<qtext>The Battle of Gettysburg was fought during which war?</qtext>
<correctAnswer>C</correctAnswer>
<eno>3</eno>
<qno>2</qno>
<qtext>Neil Armstrong and Buzz Aldrin walked how many
minutes on the moon in 1696?</qtext>
<correctAnswer>B</correctAnswer>
<eno>3</eno>
<qno>3</qno>
<qtext>Which Presidents held office during World War II?</qtext>
<correctAnswer>D</correctAnswer>
<eno>3</eno>
<qno>4</qno>
<qtext>In a communist economic system, people:</qtext>
<correctAnswer>B</correctAnswer>
<eno>3</eno>
<qno>5</qno>
<qtext>Which president did not die while in office?</qtext>
<correctAnswer>D</correctAnswer>
<eno>3</eno>
<qno>6</qno>
<qtext>Which state refused to attend the Constitutional Convention
in 1787 because it didn't want the United States government
to interfere with already established state affairs?</qtext>
<correctAnswer>A</correctAnswer>
<eno>3</eno>
<qno>7</qno>
<qtext>Who founded Buddhism?</qtext>
<correctAnswer>A</correctAnswer>
<eno>3</eno>
<qno>8</qno>
<qtext>Where is India?</qtext>
<correctAnswer>D</correctAnswer>
<eno>3</eno>
<qno>9</qno>
<qtext>What is the dominant religion in India?</qtext>
<correctAnswer>B</correctAnswer>
<eno>3</eno>
<qno>10</qno>
<qtext>Near which river did archaeologists find India's
first civilization?</qtext>
<correctAnswer>B</correctAnswer>
</question>
</Assignment3>
3.
基础历史
10
3.
3.
1.
葛底斯堡战役是哪场战争?
C
3.
2.
尼尔·阿姆斯特朗和巴兹·奥尔德林走了多少路
1696年登上月球的时间?
B
3.
3.
哪些总统在第二次世界大战期间任职?
D
3.
4.
在共产主义经济体系中,人们:
B
3.
5.
哪位总统在任期间没有死亡?
D
3.
6.
哪个州拒绝参加制宪会议
1787年,因为它不想要美国政府
干涉已经确立的国家事务?
A.
3.
7.
谁创立了佛教?
A.
3.
8.
印度在哪里?
D
3.
9
印度的主要宗教是什么?
B
3.
10
考古学家在哪条河附近发现了印度的
第一文明?
B
以下是我的源代码:
Node listOfQuestions = doc.getElementsByTagName("question").item(0);
NodeList qItem =listOfQuestions.getChildNodes();
//FOR QUESTION TABLE
for(int s=0; s<qItem.getLength(); s++)
{
int eno=0;
int qno=0;
String qtext=null;
String correctAnswer=null;
Node item=qItem.item(s);
NodeList itemChildren =item.getChildNodes();
for(int i=0;i<itemChildren.getLength();i++)
{
Node itemChild =itemChildren.item(i);
if("eno".equals(itemChild.getNodeName())) {
eno=Integer.parseInt(itemChild.getNodeValue());
System.out.println(eno);
}
if("qno".equals(itemChild.getNodeName())) {
qno=Integer.parseInt(itemChild.getNodeValue());
System.out.println(qno);
}
if("qtext".equals(itemChild.getNodeName())) {
qtext=itemChild.getTextContent();
System.out.println(qtext);
}
if("correctAnswer".equals(itemChild.getNodeName())) {
correctAnswer=itemChild.getTextContent().trim();
System.out.println(correctAnswer);
}
}
int i=st.executeUpdate("insert into a3_2.question(eno,qno,qtext,correctAnswer) values('"+eno+"','"+qno+"','"+qtext+"','"+correctAnswer+"')");
}
节点listOfQuestions=doc.getElementsByTagName(“问题”)。项(0);
NodeList qItem=listOfQuestions.getChildNodes();
//问题表
对于(int s=0;这是吗?@Michael sqlbot是的。我需要帮助这看起来不像是xml/dom问题,因为错误来自MySQL。当您插入answeroption时,这似乎发生了,但我在代码中没有看到。这是吗?@Michael sqlbot是的。我需要帮助这看起来不像是xml/dom问题,因为或者来自MySQL。当您插入到answeroption时,似乎会发生这种情况,但我在代码中没有看到。