albfernandez / javadbf

Java library for reading and writing Xbase (dBase/DBF) files.
GNU Lesser General Public License v3.0
224 stars 98 forks source link

Appending Records not working #115

Closed edgarjoao closed 10 months ago

edgarjoao commented 1 year ago

Hi there, As per the Readme file, javadbf library support appending records to an existing DBF, but I'm getting below issue try to do it.

com.linuxense.javadbf.DBFException: Fields should be set before adding records

Taking a look in the code, seems like addRecord method is preventing us to append rows without providing the headers.

https://github.com/albfernandez/javadbf/blob/main/src/main/java/com/linuxense/javadbf/DBFWriter.java#L288

If I try adding the fields it does delete all data and insert the new one :(

Any advice here? Thanks

albfernandez commented 1 year ago

Hi

Are you using the DBFWriter constructor using the "appending records" as noted in https://github.com/albfernandez/javadbf#appending-records

This constructor should populate the headers field in https://github.com/albfernandez/javadbf/blob/0c02b5fc159cea0d47ebe283b67f9fffec382b81/src/main/java/com/linuxense/javadbf/DBFWriter.java#L179

Your file is a valid dbf file? or is an empty file? Appending method only work with a valid existing dbf file, and takes the fields definition from this file

edgarjoao commented 1 year ago

Hi @albfernandez I don't think the dbf is the issue, because I can read it.

Here is the code.

`import java.io.FileOutputStream; import java.util.Date;

import com.linuxense.javadbf.DBFException; import com.linuxense.javadbf.DBFUtils; import com.linuxense.javadbf.DBFWriter;

public class WriteTest {

public static void main(String[] args) {

    String filePath = "lote.dbf";

    DBFWriter writer = null;

    try {

        writer = new DBFWriter(new FileOutputStream(filePath));

        Object rowData[] = new Object[8];
        rowData[0] = "LOTE12345";
        rowData[1] = "PEDTEST";
        rowData[2] = "PEDTEST";
        rowData[3] = new Date();
        rowData[4] = "";
        rowData[5] = "";
        rowData[6] = new Integer(1);
        rowData[7] = "";

        writer.addRecord(rowData);

    } catch (DBFException e) {
        e.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        DBFUtils.close(writer);
    }
}

}` I'm also attaching the dbf.

lote.zip

albfernandez commented 1 year ago

Hi You're using the wrong constructor, using outputSteram you're overwritting the file and never read from it.

You have to use the DBFWriter(File) construtor:

writer = new DBFWriter(new File(filePath));
edgarjoao commented 1 year ago

Hi @albfernandez You are right, I was using the wrong constructor, now it's working!! Is there a way to update a row in an existing DBF?

Thank you, Edgar

edgarjoao commented 1 year ago

seems like update functionality is not available yet. #41

albfernandez commented 1 year ago

seems like update functionality is not available yet. #41

It's a very complex functionalty, and requires a big redesign and api change