SQL:列计数与第1行的值计数不匹配
问题描述:
我在我的sql querry上有一个错误,但我不确定错误原因是什么:
Hi, i geth an erros on my sql querry, but I'm not sure why the error is:
Column count doesn't match value count at row 1
错误在线:
The error is on the line:
stmt.executeQuerry(sql1)
所以我认为有一个sql1 querry中的错误。
如果我以正确的方式解释它意味着我指定的列数量太少或太多而不是我尝试输入的值数量,但我计算和叙述它们似乎没问题。这是代码(查询是字符串sql):
so I assumed that there is an error in the sql1 querry.
If I interpret it the right way it means that I specify too less or too much columns compare to the number of values I try to enter, but I counted and recounted them and it seems to be ok. Here is the code (the query is the string sql):
String[] array = value.split("=");
int credit = array.length-1;
java.sql.Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e1) {
e1.printStackTrace();
}
String sql = ("INSERT INTO std_details (StudentID, LastName, FirstName,"+
"Initial, City, State,"+
"Phone, Gender, Year,"+
"Major, Credits, CGPA)"+
"VALUES('"
+ array[1] + "','"
+ array[5] + "','"
+ array[4] + "','"
+ array[2] + "','"
+ array[7] + "','"
+ array[8] + "','"
+ array[6] + "','"
+ array[3] + "','"
+ array[array.length-3] + "','"
+ array[array.length-1] + "','"
+ array[array.length-2] + "','"
+ array[array.length-4] + "');");
String sql1 = ("INSERT INTO csr_courses (StudentID"+
", Course_1, Grade_1, CourseCredit_1"+
", Course_2, Grade_2, CourseCredit_2"+
", Course_3, Grade_3, CourseCredit_3"+
", Course_4, Grade_4, CourseCredit_4"+
", Course_5, Grade_5, CourseCredit_5) "
+"VALUES('"
+ array[1] + "','"
+ array[9] + "','"
+ array[10] + "','"
+ array[11] + "','"
+ array[12] + "','"
+ array[13] + "','"
+ array[14] + "','"
+ array[15] + "','"
+ array[16] + "','"
+ array[17] + "','"
+ array[18] + "','"
+ array[19] + "','"
+ array[20] + "','"
+ array[21] + "','"
+ array[22] + "');");
if (duplicate(array[1].toString())){
try {
System.out.println("found ducplicate");
os.writeObject("AddSudent duplicate");
os.flush();
os.reset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
stmt.executeUpdate(sql);
stmt.executeUpdate(sql1);
stmt.close();
os.writeObject("AddSudent ok");
os.flush();
os.reset();
} catch (SQLException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
try {
os.writeObject("AddSudent fail");
os.flush();
os.reset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答
您指定16
columns(StudentID
加上5 * 3
课程详情),同时仅传递15
值(缺少课程详情之一)。
You are specifying16
columns (StudentID
plus5 * 3
course details) while passing only15
values (that is one of the course details is missing).
计算您的字段,然后计算您的值:
Count your fields, then count your values:
String sql1 = ("INSERT INTO csr_courses (StudentID"+
", Course_1, Grade_1, CourseCredit_1"+
", Course_2, Grade_2, CourseCredit_2"+
", Course_3, Grade_3, CourseCredit_3"+
", Course_4, Grade_4, CourseCredit_4"+
", Course_5, Grade_5, CourseCredit_5) "
+"VALUES('"
+ array[1] + "','"
+ array[9] + "','"
+ array[10] + "','"
+ array[11] + "','"
+ array[12] + "','"
+ array[13] + "','"
+ array[14] + "','"
+ array[15] + "','"
+ array[16] + "','"
+ array[17] + "','"
+ array[18] + "','"
+ array[19] + "','"
+ array[20] + "','"
+ array[21] + "','"
+ array[22] + "');");
16个字段,15个值...
16 fields, 15 values...
您的sql1查询定义了16个列,但在VALUES部分中只显示了15个列。
Your sql1 query has 16 columns defined but only 15 columns are represented in the VALUES section.