Menu

"java.lang.IllegalArgumentException: invalid fixed length size" error when creating table

Help
2015-01-29
2015-01-31
  • Francesco Brundu

    Hi,
    I need help in a routine I've written to dump the content of a class (which represent a database table) to a new database table in MS Access. My code is the following:

    public void dumpDB() throws IOException, Exception {
        // for each table
        for (String tableName : this.DB.getTablesNames()) {
            System.out.println(tableName);
            int nColumns = 0;
            ModelDatabaseTable table = this.DB.getTable(tableName);
    
            // create a tablebuilder
            TableBuilder DBTableBuilder = new TableBuilder(tableName);
    
            // get datatypes of columns
            Map<String, DataType> columns = table.getColumns();
    
            // for each column
            for (String columnName : columns.keySet()) {
                System.out.println(columnName);
    
                // get its datatype
                DataType dt = columns.get(columnName);
    
                // create a column with correspondent datatype and max length and add it
                // to the table builder
                ColumnBuilder cb = new ColumnBuilder(columnName).setType(dt).setMaxLength();
                DBTableBuilder.addColumn(cb);
                nColumns += 1;
            }
            // if table has columns
            if (nColumns > 0) {
    
                // save it to the actual database: Exception rises here
                Table DBTable = DBTableBuilder.toTable(this.DBConnection);
    
    
                // copy all table's rows
                for (ModelDatabaseRow row : table.getRows()) {
                    List<String> values = new ArrayList<String>();
                    for (String columnName : columns.keySet()) {
                        String columnValue = row.getColumn(columnName);
                        values.add(columnValue);
                    }
                    DBTable.addRow(values.toArray());
                }
            }
        }
    }
    

    When I try to save the table to the actual database, I get the exception:

    java.lang.IllegalArgumentException: invalid fixed length size
    at com.healthmarketscience.jackcess.ColumnBuilder.validate(ColumnBuilder.java:361)
    at com.healthmarketscience.jackcess.impl.TableCreator.validate(TableCreator.java:207)
    at com.healthmarketscience.jackcess.impl.TableCreator.createTable(TableCreator.java:130)
    at com.healthmarketscience.jackcess.impl.DatabaseImpl.createTable(DatabaseImpl.java:954)
    at com.healthmarketscience.jackcess.TableBuilder.toTable(TableBuilder.java:223)
    at modelDatabase.AccessModelDatabaseBuilder.dumpDB(AccessModelDatabaseBuilder.java:153)
    at modelDatabase.AccessModelDatabaseBuilder.main(AccessModelDatabaseBuilder.java:37)
    

    DataTypes were saved before using the same database I am writing (I am basically updating the database), using the code:

    for (Column column : DBTable.getColumns()) {
        table.addColumn(column.getName(), column.getType(), "");
    }
    

    What am I doing wrong?

    Thanks,

     

    Last edit: Francesco Brundu 2015-01-29
  • James Ahlborn

    James Ahlborn - 2015-01-29

    ColumnBuilder.setMaxLength() is probably not as smart as it could be. It's only really valid for variable length columns. To fix your code, use something like:

    if(dt.isVariableLength()) {
      cb.setMaxLength();
    }
    
     
  • Francesco Brundu

    Thank you!

    Is there a particular advantage of using dt.isVariableLength() instead of dt.isTrueVariableLength()?

    Thanks

     
    • James Ahlborn

      James Ahlborn - 2015-01-29

      for this specific code block, either one would work.

       
  • Gord Thompson

    Gord Thompson - 2015-01-29

    ColumnBuilder.setMaxLength() is probably not as smart as it could be.

    Would you have any objections to someone submitting a ticket with a request to make it smarter? :)

     
    • James Ahlborn

      James Ahlborn - 2015-01-30

      Saves me from writing it myself!

       
    • James Ahlborn

      James Ahlborn - 2015-01-31

      Filed as https://sourceforge.net/p/jackcess/bugs/116/, will be in the 2.0.9 release.

       

Log in to post a comment.

MongoDB Logo MongoDB