Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)


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):

Java
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();
                }
            }
        }
Posted
Comments
Richard Deeming 19-Dec-14 8:44am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

You are specifying 16 columns (StudentID plus 5 * 3 course details) while passing only 15 values (that is one of the course details is missing).
 
Share this answer
 
Comments
Member 11322545 19-Dec-14 8:22am    
I guess I've spent much time in front of a screen for today lol, been counting this for a least one hour !
CPallini 19-Dec-14 8:24am    
And numbered array indices didn't help you... :-)
Count your fields, then count your values:
Java
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 fields, 15 values...
 
Share this answer
 
Your sql1 query has 16 columns defined but only 15 columns are represented in the VALUES section.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900