Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.sql.SQLException: Column count doesn't match value count at row 1 [duplicate]

Tags:

java

mysql

jdbc

I'm trying to update values using JDBC and I continue to get the same error for different tables and with different schemas.

Let's say that I have a table like this

+----------------+-------------+------+-----+---------+-------+
| Field          | Type        | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| field1         | varchar(50) | YES  |     | NULL    |       |
| field2         | varchar(50) | YES  |     | NULL    |       |
+----------------+-------------+------+-----+---------+-------+

then, I try to add a row:

String Text1 = text1;
String Text2 = text2;
String Query_String = "INSERT INTO tablename(field1,field2) VALUES ('"+Text1+","+Text2+"')";
Query_Statement.executeUpdate(Query_String);

the number of columns is the same, and also in the text there are not other commas, but I continue to get the error "java.sql.SQLException: Column count doesn't match value count at row 1"

I'm sure it's something simple, probably on the syntax since I managed to make it to work with a single column...

Thanks in advance

like image 481
Andrew Strathclyde Avatar asked Oct 25 '10 11:10

Andrew Strathclyde


People also ask

How to fix Column count doesn t match value count at row 1?

To resolve this “Column count doesn't match value count at row 1” error, you have to ensure that the columns in the table or your INSERT statement match the values you are inserting. The best way to do this is to specify the columns you want in your INSERT statement and ensure the VALUES clause matches the column list.

How do I fix error code 1136?

To fix this issue, make sure you're inserting the correct number of columns into the table. Alternatively, you can name the columns in your INSERT statement so that MySQL knows which columns your data needs to be inserted into.


3 Answers

There is something wrong with:

String Query_String = "INSERT INTO tablename(field1,field2) VALUES ('"+Text1+","+Text2+"')";

You've missed some quotes between Text1 and Text2:

String Query_String = "INSERT INTO tablename(field1,field2) VALUES ('"+Text1+"','"+Text2+"')";
like image 99
Giann Avatar answered Sep 25 '22 03:09

Giann


String Query_String = "INSERT INTO tablename(field1,field2) VALUES ("'"+Text1+"' , '"+Text2+"');";  

It should like this note ' this

PreparedStatement would be better choice.

like image 25
jmj Avatar answered Sep 24 '22 03:09

jmj


you have got a mistake with your quotes...

the following will be executed:

INSERT INTO tablename(field1,field2) VALUES ('Text1,Text2'); 

you have to write:

String Query_String = "INSERT INTO tablename(field1,field2) 
VALUES ('"+Text1+"','"+Text2+"')"

like image 21
Tobias Bambullis Avatar answered Sep 24 '22 03:09

Tobias Bambullis