mysql - SQLException 捕获 : Column count doesn't match value count at row

标签 mysql calculated-columns

我的 MySQL 数据库突然出现问题。我的数据库中有以下列:

P_id INT NOT NULL AUTO_INCREMENT,
Date DATE NOT NULL,
Name VARCHAR(32) NOT NULL,
Address VARCHAR(32) NOT NULL,
Day_hours INT NOT NULL,
Day_minutes INT NOT NULL,
Km_to_address INT NOT NULL,
Time_to_address INT NOT NULL,
Allday_hours INT
PRIMARY KEY(P_id)

在我的 servlet 中,我发送以下请求:

Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(connectionURL, "root", ""); 
            String sql = "INSERT INTO Workdata VALUES (?,?,?,?,?,?,?)"; 
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, Date);
            pst.setString(2, Name);
            pst.setString(3, Address);
            pst.setString(4, Day_hours);
            pst.setString(5, Day_minutes);
            pst.setString(6, Km_to_address);
            pst.setString(7,  Time_to_address);

            pst.executeUpdate();
            pst.close();

关键是我将我的 P_id 作为主键,每次我添加一些数据时它应该自动计数。如您所见,我没有 setString 到 Allday_hours。我使用此列来计算 Day_hours 和 Day_minutes。我通过查询执行此操作:

SELECT *, (Day_hours + (Day_minutes / 100)) as Allday_hours FROM Workdata

这样做的目的是为了让我的女朋友一天在不同的地方工作。因此她可以在 5 个不同的地方输入 fx,然后 Allday_hours 计算她一天的工作量。但是当我将数据放入 JSP 站点时,出现错误:

捕获到 QLException:列数与第 1 行的值数不匹配

我猜想这与我的 Servlet 有关,我在其中设置了 setString(1, Date)。我尝试将其设置为 setString(2, date) 并将其他值更改为 3、4、5 等。

有人能看出我在这里做错了什么吗? 向大家致以最诚挚的问候和美好的一天 来自疯狂

最佳答案

我认为您收到错误是因为您的表实际上有 9 列,但在您的查询中您只插入了 7 个值而没有插入查询中涉及的 put 列,因此最好在查询中指定将使用哪个列。 另外,我认为您的列 Allday_hours 不应该出现在表中,因为 since 将由您选择的查询创建为虚构的。您的 P_id 列可以在插入查询中避免,因为它是一个自动增量列,因此它将自行插入其值

String sql = "INSERT INTO Workdata (date, name, address...) VALUES (?,?,?,?,?,?,?)"; 

关于mysql - SQLException 捕获 : Column count doesn't match value count at row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22198308/

相关文章:

python - 使用python在django中将数据库转换为csv

ms-access - 遍历表和更改值时 Access 崩溃

php - 从数据库mysql中查找最小值

php - Symfony 2 中的联接表

PHP/MySQL 插入查询

java - 我在通过 Apache POI 添加计算字段时遇到问题

python - 删除 Pandas Dataframe 列范围内每列总和小于 10 的列

javascript - 从 mySQL 渲染 HTML 以显示在页面上 Angular 和 JavaScript

arrays - Spotfire 数据表中的上一行值

sql-server - SQL Server : Is there a performance cost to computed columns?