Error when inserting new record in BD

0

I have a problem which is that I try to run an SQL statement from my program and it fails 95% of the time, and try to execute the statement from the SQLmanager and it works, try to find out if the connection to the DB was closed during the execution and it is open, try the option "Clean and Build Project" that I do not even remember for what it was but I did it and then restart the PC and nothing. The "select" sentences seem to work without problems.

I have come to think that talves is something that I played a few hours ago, I was moving the computer and it seems that Netbeans started to process something and I gave it up.

The process covers several classes so I will try to put as little code as possible, sorry for the mess.

//Clase FrmFacturas
public void guardarFactura(){
        if(this.tblDetalle.getRowCount()>0 && this.txtCliente.isEnabled()==false && this.idVendedor > 0){
            Calendar fecha = Calendar.getInstance();
            String anno = String.valueOf(fecha.get(Calendar.YEAR)), mes = String.valueOf((fecha.get(Calendar.MONTH)+1)), dia = String.valueOf(fecha.get(Calendar.DAY_OF_MONTH));
            if(mes.length()==1){
                mes = "0"+anno;
            }
            if(dia.length()==1){
                dia = "0"+dia;
            }
            List<DetalleProdFact> detProds = new ArrayList<>();
            for(int x=0; x<this.tblDetalle.getRowCount(); x++){
                    int exG=0;
                    if(String.valueOf(this.tblDetalle.getValueAt(x, 3)).equals("Gravado")){
                        exG = 1;
                    }else if(String.valueOf(this.tblDetalle.getValueAt(x, 3)).equals("Exento")){
                        exG = 2;
                    }
                    detProds.add(new DetalleProdFact(0, this.idDet.get(x), Integer.valueOf(String.valueOf(this.tblDetalle.getValueAt(x, 0))), Integer.valueOf(String.valueOf(this.tblDetalle.getValueAt(x, 4))), exG, Integer.valueOf(String.valueOf(this.tblDetalle.getValueAt(x, 2)))));
            }
            int porcDesc;
            if(this.txtIngresarDescuento.getText().matches("\d+")){
                porcDesc = Integer.valueOf(this.txtIngresarDescuento.getText());
            }else{
                porcDesc = 0;
            }
            fact = new Factura(anno+"-"+mes+"-"+dia, this.txtOrdenCompra.getText(), this.idVendedor, porcDesc, Integer.valueOf(this.txtTotal.getText()), detProds);
            if(fact.guardar()==true){
                this.limpiarFact();
            }else{
                JOptionPane.showMessageDialog(rootPane, "Error al guardar la factura");
            }
        }
    }

//Clase Factura, en esta clase el GetGeneratedKeys().getInt(1) (BD.obtenerUltimoId) devuelve un id adelante como si se hubiera guardado en la BD pero esto no se refleja en la BD.
public boolean guardar(){
        BD.conectar();
        BD.ejecutarSqlUpdate("insert into factVentas values (Null, '"+this.fecha+"', '"+this.orden+"', "+this.vendedor+", "+this.descuento+", "+this.total+");");
        this.id=BD.obtenerUltimoId();
        if(id>0){
            for(int x=0; x<this.detProd.size(); x++){
                this.detProd.get(x).setFactura(this.id);
                this.detProd.get(x).guardar();
            }
            BD.desconectar();
            return true;
        }
        BD.desconectar();
        return false;
    }

//Clase BaseDatos
public void ejecutarSqlUpdate(String sql){
        try {
            this.sql.executeUpdate(sql);
        } catch (SQLException ex) {
            System.out.println("Error en SQLUpdate: "+sql);
//Un ejemplo es "Error en SQLUpdate: insert into factVentas values (Null, '2017-10-02', '', 6, 0, 1792);"
        }
    }

oct 02, 2017 4:01:12 PM Datos.BaseDatos ejecutarSqlUpdate
GRAVE: null
java.sql.SQLException: database is locked
    at org.sqlite.NativeDB.throwex(NativeDB.java:210)
    at org.sqlite.NativeDB._exec(Native Method)
    at org.sqlite.Stmt.executeUpdate(Stmt.java:152)
    at Datos.BaseDatos.ejecutarSqlUpdate(BaseDatos.java:78)
    at Logica.Factura.guardar(Factura.java:171)
    at Grafica.FrmFacturas.guardarFactura(FrmFacturas.java:345)
    at Grafica.FrmFacturas.btnGuardarActionPerformed(FrmFacturas.java:868)
    at Grafica.FrmFacturas.access$3800(FrmFacturas.java:46)
    at Grafica.FrmFacturas$19.actionPerformed(FrmFacturas.java:688)

oct 02, 2017 4:01:12 PM Datos.BaseDatos ejecutarSqlUpdate
GRAVE: null
java.sql.SQLException: foreign key constraint failed
    at org.sqlite.NativeDB.throwex(NativeDB.java:210)
    at org.sqlite.NativeDB._exec(Native Method)
    at org.sqlite.Stmt.executeUpdate(Stmt.java:152)
    at Datos.BaseDatos.ejecutarSqlUpdate(BaseDatos.java:78)
    at Logica.DetalleProdFact.guardar(DetalleProdFact.java:136)
    at Logica.Factura.guardar(Factura.java:176)
    at Grafica.FrmFacturas.guardarFactura(FrmFacturas.java:345)
    at Grafica.FrmFacturas.btnGuardarActionPerformed(FrmFacturas.java:868)
    at Grafica.FrmFacturas.access$3800(FrmFacturas.java:46)
    at Grafica.FrmFacturas$19.actionPerformed(FrmFacturas.java:688)
    
asked by ANoobie 02.10.2017 в 12:21
source

1 answer

0

I already solved the errors, the solution to the first one was to check all the instances of BD to try to find some open connection because it seems that among the limitations of SQLite is that you can not add / update records to a table that already has a open connection either select or insert.

And the second "solved" alone, when trying to get the last Id generated in the BD gave me a number in front of the last one as if the record I was trying to create had been created correctly when in fact it was not like that.

    
answered by 03.10.2017 / 10:02
source