yortus/DBFFile

Problem to add records to existing dbf file

Closed this issue · 4 comments

I have been working with this excellent library so now I am having a problem to save a new record to existing dbf file, please let me know if you have any solutions

@victortyau you'll need to provide more info. What errors are you getting? Steps to reproduce? Sample file?

var DBFFILE = require("dbffile");


var fieldDescriptors = [
    { name: 'ncliente', type: 'C', size: 5 },
    { name: 'tipobol', type: 'N', size: 1 },
    { name: 'fechabol', type: 'D', size: 8 },
    { name: 'comentab1', type: 'C', size: 60 },
    { name: 'comentab2', type: 'C', size: 60 },
    { name: 'comentab3', type: 'C', size: 60 },
    { name: 'usuario', type: 'C', size: 3 },
    { name: 'horatran', type: 'C', size: 8 }
];

var rows = [{
    ncliente: '0001',
    tipobol: 1,
    fechabol: "28/9/2017",
    comentab1: "Prueba Nodejs",
    comentab2: "Prueba Nodejs",
    comentab3: "Prueba Nodejs",
    usuario: "hectol",
    horatran: "8:30"
}];

DBFFILE.open("BOLETASF.DBF", fieldDescriptors)
    .then(dbf => {
        console.log(dbf.recordCount);

    })
    .then(dbf => {
        dbf.append(rows);
    })
    .catch(err => console.log('An error occurred: ' + err));

I want to add new record, I need to open not create, let me know, how can solve this issue?, thanks

@victortyau try this:

var DBFFILE = require("dbffile");

var rows = [{
    ncliente: '0001',
    tipobol: 1,
    fechabol: "28/9/2017",
    comentab1: "Prueba Nodejs",
    comentab2: "Prueba Nodejs",
    comentab3: "Prueba Nodejs",
    usuario: "hectol",
    horatran: "8:30"
}];

DBFFILE.open("BOLETASF.DBF")
    .then(dbf => {
        return dbf.append(rows);
    })
    .catch(err => console.log('An error occurred: ' + err));

When you open an existing DBF file, the field descriptors are read from the file. You don't need to list them yourself. If the rows you are appending match the fields in the DBF file, append should work.

Thanks so much mate, I can add new rows to dbf file