albfernandez / javadbf

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

SmallInt fieldtype is not suppoted? #105

Closed JosepMariaGomez closed 2 years ago

JosepMariaGomez commented 2 years ago

I'm trying to write a DBF with smallInt field, but is not supported. How can I write it?

Many thanks and good work!!

albfernandez commented 2 years ago

As far as I know you can not write smallint in dbf files. Could you provide a sample flie or a link to some specification including that smallint datatype?

JosepMariaGomez commented 2 years ago

Hi Alberto, many thanks for the answer. I put 2 images from a DBFFile with Smallint fields.

2022-03-16_18-37 2022-03-16_18-37_1

Muchas gracias!

albfernandez commented 2 years ago

I'm surprised, dbaseIII doesn't have a "smallint" datatype, maybe your driver / program is using this as an alias for a dbase type.

Could you try to read your file with this example code to print table structure to console


public static void main(String[] args) throws Exception {
    writeToConsole(new File(args[0]));
}
public static void writeToConsole(File file) throws FileNotFoundException {
    DBFReader reader = null;
    try {
        reader = new DBFReader(new BufferedInputStream(new FileInputStream(file)));
            int numberOfFields = reader.getFieldCount();

        for (int i = 0; i < numberOfFields; i++) {
            DBFField field = reader.getField(i);
            System.out.println(field.getType() + " (" + field.getLength() + ") "+ field.getName());
        }

    }
    finally {
        DBFUtils.close(reader);
    }
}

If the output doesn't clarify the datatype used for "smallint", you could send a sample file without confidential data.

JosepMariaGomez commented 2 years ago

Thanks Alberto,

done!!

Result: ... IDNIF NUMERIC (1,0) CODPAIS CHARACTER (2,0) REP14NIF CHARACTER (9,0) REP14NOM CHARACTER (40,0) METCOBRO LOGICAL (1,0) METCOBFRE LOGICAL (1,0) SUPLIDO LOGICAL (1,0) PROVISION LOGICAL (1,0) LESIRPF LOGICAL (1,0) NIRPF NUMERIC (16,2) NCLAVEIRPF NUMERIC (2,0) LESMOD130 LOGICAL (1,0) LDEDUCIBLE LOGICAL (1,0)

IDNIF and NCLAVEIRPF are defined as NUMERIC(1,0) and Numeric(2,0) and are readed by Vitaly Levchencko's Database Tour as SmallInt...

I've tried also to read the structure with Borland Database Desktop and the result is the same as with your driver says:

2022-03-17_16-55

I'll try to create them like Numeric(1,0) and Numeric(2,0) and to see if are readed by the external app.

Thanks a Lot!!!

JosepMariaGomez commented 2 years ago

Hi Alberto, creating fields as javadbf says, the fields are created well and moderns programs thats opens the dbf, it reads as Smallint.

Many thanks.