zoit
zoit

Reputation: 667

Problems with preparedStatement in an insert query

I have a table in a MySQL database which is 13 columns, the first one is auto-increment and the rest are normal, I have this:

    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();

I have the next mistake: java.sql.SQLException: Column count doesn't match value count at row 1, why?

The column which is auto-increment it's not here obviously, so without it, they are 12 columns.

Upvotes: 0

Views: 882

Answers (3)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79979

The error you posted is very clear, the column names that you listed doesn't match the values listed in the VALUES clause, thats probably because you missed out the , in the end of the first line, try this instead:

                                           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("+             
            "?,?,?,?,?,?,?,?,?,?,?,?);";

Upvotes: 3

kosa
kosa

Reputation: 66667

You have only 11 fields

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)

Upvotes: 0

podiluska
podiluska

Reputation: 51514

You have 11 fields in your names, but 12 question marks.

You're probably missing a , at the end of the first line.

Upvotes: 1

Related Questions