【问题标题】:Problems with preparedStatement in an insert query插入查询中的preparedStatement问题
【发布时间】:2012-09-05 14:32:27
【问题描述】:

我在 MySQL 数据库中有一个 13 列的表,第一个是自动递增的,其余的都是正常的,我有这个:

    SQL="INSERT INTO cuentas (id_proveedor_cloud,id_esquema_asociado,id_tipo_cuenta,n_cuenta,nombre_cuenta,cod_cliente"+ 
            "descuento,f_creacion,borrado,f_borrado,id_cuenta_ccis,logo)"+
            " VALUES("+             
            "?,?,?,?,?,?,?,?,?,?,?,?);";

            this.pstm = this.conexion.prepareStatement(SQL);
            this.pstm.setInt(1, proveedor);
            this.pstm.setInt(2, esquema);
            this.pstm.setInt(3, cuenta.getNivel());
            this.pstm.setString(4,num_c);
            this.pstm.setString(5, cuenta.getNombre_cuenta());
            this.pstm.setString(6, "");
            this.pstm.setDouble(7, cuenta.getDescuento());
            this.pstm.setString(8,dateFormat.format(cal.getTime()).toString());
            this.pstm.setBoolean(9, cuenta.isBorrado());
            if(cuenta.isBorrado()==false){
               this.pstm.setString(10,null);
            }else{
                this.pstm.setString(10, dateFormat.format(cal.getTime()).toString());
            }
            this.pstm.setInt(11, cuenta.getId_cuenta_padre());
           this.pstm.setBytes(12, cuenta.getLogo());
int ejecutado =  this.pstm.executeUpdate();

我有下一个错误:java.sql.SQLException: Column count doesn't match value count at row 1,为什么?

自增的列显然不在这里,所以没有它,它们是12列。

【问题讨论】:

  • cod_clientedescuento 是两列还是一列?在您的字符串中,它们 似乎 是一列(没有逗号),但这意味着您只有 11 个而不是 12 个。
  • 谢谢,正因为如此。

标签: java mysql sql prepared-statement


【解决方案1】:

您发布的错误非常清楚,您列出的列名与VALUES 子句中列出的值不匹配,那可能是因为您错过了第一行末尾的,,请尝试改为:

                                           You missed this , here------
                                                                      |
INSERT INTO cuentas (id_proveedor_cloud,id_esquema_asociado,         \|/
                     id_tipo_cuenta,n_cuenta,nombre_cuenta,cod_cliente, 
            descuento,f_creacion,borrado,f_borrado,id_cuenta_ccis,logo
VALUES("+             
            "?,?,?,?,?,?,?,?,?,?,?,?);";

【讨论】:

    【解决方案2】:

    您的姓名中有 11 个字段,但有 12 个问号。

    您可能在第一行末尾缺少,

    【讨论】:

      【解决方案3】:

      你只有 11 个字段

      cuentas (id_proveedor_cloud,
      id_esquema_asociado,
      id_tipo_cuenta,
      n_cuenta,
      nombre_cuenta,
      cod_clientedescuento,
      f_creacion,
      borrado,
      f_borrado,
      id_cuenta_
      ccis,logo)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-27
        • 1970-01-01
        • 1970-01-01
        • 2011-01-20
        相关资源
        最近更新 更多