java.sql.SQLException:列计数与第 1 行的值计数不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4014068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:24:34  来源:igfitidea点击:

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

javamysqljdbc

提问by Andrew Strathclyde

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

我正在尝试使用 JDBC 更新值,但对于不同的表和不同的架构,我继续遇到相同的错误。

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"

列数相同,并且在文本中也没有其他逗号,但我继续收到错误“java.sql.SQLException:列计数与第 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

提前致谢

采纳答案by Giann

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:

您错过了 Text1 和 Text2 之间的一些引号:

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

回答by Jigar Joshi

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

It should like this note 'this

这个应该注意'这个

PreparedStatementwould be better choice.

PreparedStatement会是更好的选择。

回答by Tobias Bambullis

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+"')"