Column count doesn't match value count at row

2019-07-23 15:57发布

问题:

I am having the following error:

Column count doesn't match value count at row 1

and my code is:

Connection             con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/BFPL","root","ilovepepsi");
PreparedStatement ps=con.prepareStatement("Insert into User values(?,?,?,?,?,?)");
ps.setString(1,t52.getText());
ps.setString(2,pw2.getText());
ps.setString(3,t53.getText());
ps.setString(4,t54.getText());
ps.setInt(5,100);
ps.setInt(6,11);
ps.executeUpdate();
PreparedStatement ps1=con.prepareStatement("Insert into User_Team values(?,?)");
ps1.setString(1,t52.getText());
ps1.setInt(2,0);
ps1.executeUpdate();
con.close();

in first table user their are 6 columns, and in 2nd table User_team their are 13 columns in which i am inserting just 2 values from which 1st value is primary key.

回答1:

If you are not inserting in all columns of a table then you have to name the columns you are inserting into in your insert query like this

Insert into User_Team (col1, col4) values(?,?)

Think about it - how should the DB engine know which columns the values belong to?



回答2:

PreparedStatement ps1=con.prepareStatement("Insert into User_Team (col_name1, col_name2) values(?,?)");


标签: mysql jdbc