[Gvsig_english] Error in table creation (fieldType ignored)
Flavio Pompermaier
fla83tn at gmail.com
Fri Apr 22 08:56:16 CEST 2011
I found the solution... I have to use the following code:
//-------------Crear el dbf----------------------
DbaseFileHeaderNIO myHeader =
DbaseFileHeaderNIO.createDbaseHeader(fields);
myHeader.setNumRecords(0);
DbaseFileWriterNIO dbfWrite = new DbaseFileWriterNIO(myHeader,
(FileChannel) getWriteChannel(tableFile.getAbsolutePath()));
dbfWrite.close();
//------------Añadir el dbf al proyecto--------------
ProjectExtension ext = (ProjectExtension)
PluginServices.getExtension(ProjectExtension.class);
String name = tableFile.getName();
LayerFactory.getDataSourceFactory().addFileDataSource("gdbms dbf
driver", name, tableFile.getAbsolutePath() );
DataSource dataSource =
LayerFactory.getDataSourceFactory().createRandomDataSource(name,
DataSourceFactory.AUTOMATIC_OPENING);
dataSource.setDataSourceFactory(LayerFactory.getDataSourceFactory());
SelectableDataSource sds = new SelectableDataSource(dataSource);
EditableAdapter auxea=new EditableAdapter();
auxea.setOriginalDataSource(sds);
ProjectTable projectTable =
ProjectFactory.createTable(tableName, auxea);
ext.getProject().addDocument(projectTable);
I hope this could help someone else..:)
2011/4/21 Flavio Pompermaier <fla83tn at gmail.com>
> Hi to all,
> I don't know if I'm doing something wrong but I have a very strange problem
> in table creation.
> I want to create an alphanumeric table (DBF) from the scratch, and the code
> I use for doing this is:
>
> Driver driver = LayerFactory.getDM().getDriver("gdbms dbf
> driver");
> dsf.createFileDataSource(driver.getName(), tableName,
> tableFile.getAbsolutePath(), campos, fieldTypes);
>
> DataSource dataSource = dsf.createRandomDataSource(tableName,
> DataSourceFactory.AUTOMATIC_OPENING);
> dataSource.setDataSourceFactory(dsf);
> SelectableDataSource sds = new
> SelectableDataSource(dataSource);
> EditableAdapter auxea = new EditableAdapter();
> auxea.setOriginalDataSource(sds);
> ProjectTable projectTables =
> ProjectFactory.createTable(tableName, auxea);
> ProjectExtension ext = (ProjectExtension)
> PluginServices.getExtension(ProjectExtension.class);
> ext.getProject().addDocument(projectTables);
>
> The problem is that in this way, suppose that I have a fieldType = 4
> (Integer), then in the created table I found a Double (fieldType=8)!!
> To build correctly my table I have to:
>
> 1 - create an empty table:
> dsf.createFileDataSource(driver.getName(), tableName,
> tableFile.getAbsolutePath(), new String[0], new int[0]);
> 2 - add a FieldDescription one at a time:
> .......
> auxea.setOriginalDataSource(sds);
> auxea.startComplexRow();
> for (int j = 0; j < fields.length; j++) {
> FieldDescription field = fields[j];
> auxea.addField(field);
> }
> auxea.endComplexRow("Fix table creation");
> ProjectTable projectTables =
> ProjectFactory.createTable(tableName, auxea);
> .......
>
> Is this a bug??
>
> Best regards,
> Flavio
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserv.gva.es/pipermail/gvsig_internacional/attachments/20110422/be34b8de/attachment.htm
More information about the Gvsig_internacional
mailing list