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

怎甘沉沦 提交于 2019-12-17 02:00:15

问题


The structure of my table:

id int AUTO_INCREMENT PRIMARY KEY
title text
url text
age int

Here's how I am trying to save data into this table:

PreparedStatement ps=con.prepareStatement("insert into table(title, url, age) values ('\"+title+\",\"+url+\",\"+age+\"')");
System.out.println("Connected database successfully..");
ps.executeUpdate(); 

But when I run the app, I get

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

I guess the problem might be in the id column, how to solve it?


回答1:


The problem is not the id column.

From the statement it looks like you have quotes around all columns. Therefore it seems to the SQL, that you have only one column

'"title","url","age"'

What you might want to have is

"insert into table(title, url, age) values ('" + title + "','" + url + "'," + age + ")"

or even better yet, since it is a prepared statement

"insert into table(title, url, age) values (?, ?, ?)"



回答2:


Actually, you have a different problem (you're only passing one "value") -

PreparedStatement ps=con.prepareStatement("insert into table(title, url, age) "
    + "values (?,?,?)");
ps.setString(1, title);
ps.setString(2, url);
ps.setInt(3, age); // <-- at a guess!

You original query put all three values in one string '\"+title+\",\"+url+\",\"+age+\"'.



来源:https://stackoverflow.com/questions/20845977/java-sql-sqlexception-column-count-doesnt-match-value-count-at-row-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!