Delete query Java & Mysql

0

I am trying to remove a record from the database with the book code I pick up from a combo and from all its associated data tables using PrepareStatement but it is throwing me an error. I pass the code to you because surely it is not doing something correctly.

private void OnEliminarLibro(ActionEvent e) {

           String aux= getRoot().getCodigoLibroEliminadosCombo().getSelectionModel().getSelectedItem();
            PreparedStatement stmtLibrosAutores = null;
            PreparedStatement stmtLibros = null;
            PreparedStatement stmtEjemplares = null;


            String consultaLibrosAutores = "DELETE codLibro FROM librosautores WHERE codLibro = ?";
            String consultaEjemplares = "DELETE codLibro FROM ejemplares WHERE codLibro = ?";
            String consultaLibros = "DELETE codLibro FROM libros WHERE codLibro = ?";

            try {
                stmtLibros = con.getCon().prepareStatement(consultaLibros);

             int result = Integer.valueOf(aux);
                 System.out.println(result);
                 stmtLibros.setInt(1,result);
                stmtLibros.executeUpdate();

                stmtLibrosAutores = con.getCon().prepareStatement(consultaLibrosAutores);
                stmtLibrosAutores.setInt(1, result);
                stmtLibrosAutores.executeUpdate();

                stmtEjemplares = con.getCon().prepareStatement(consultaEjemplares);
                stmtEjemplares.setInt(1, result);
                stmtEjemplares.executeUpdate();

                System.out.println("Eliminacion realizada");

            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
        }
}

The trace of the error that you are giving me is the following:

Below I also attach an image of the structure of the Database as a reference.

    
asked by Ricki 26.11.2016 в 01:33
source

1 answer

4

The syntax of the delete statement is incorrect, you should not include any field names; If you think about it carefully it does not make sense, since the whole row is deleted.

The correct basic syntax is:

delete from Tabla [where condiciones]

Change your sentences to be similar to:

String consultaLibrosAutores = "DELETE FROM librosautores WHERE codLibro = ?";

To explain the strange error message and, hopefully, the reader to learn something new, I will add that the engine does not give a syntax error, because mySQL supports joins in delete and you can indicate after the word reserved delete the alias of the table you want to delete, for example:

delete p
  from Pedido p
       inner join Cliente c on p.idCliente = c.idCliente
 where c.Nombre = 'Juan';

In this hypothetical case, this would erase all the customer's orders. (is what mysql calls a MULTI DELETE).

The analyzer gets confused and reports that it does not find the alias you put after the word delete .

    
answered by 26.11.2016 / 01:41
source