[Gvsig_english] How to open, modify and edit a dbasefile ?

Vicente Caballero Navarro vicente.caballero at iver.es
Thu May 17 09:26:39 CEST 2007


Hello. 

You are trying it at too low level.  You should better try it in an 
upper-level way

To create an access to a dbf you may have a look to the piece of code in 
the method createFromGUI in the class ProjectTableFactory at appgvSIG

LayerFactory.getDataSourceFactory().addFileDataSource("gdbms dbf driver",
                   name, "C:\\properties.dbf");
DataSource dataSource = LayerFactory.getDataSourceFactory()
                   .createRandomDataSource(name,
                   DataSourceFactory.AUTOMATIC_OPENING);
SelectableDataSource sds = new SelectableDataSource(dataSource);
EditableAdapter auxea = new EditableAdapter();
auxea.setOriginalDataSource(sds);


To make sql queries, in the extension called FiltroExtension we have 
methods to which the expression is passed to as a parameter and return a 
vector of long with the Selection produced by such query.

private long[] doSet(String expression) {
        try {
            DataSource ds = 
LayerFactory.getDataSourceFactory().executeSQL(expression,
                    DataSourceFactory.MANUAL_OPENING);

            return ds.getWhereFilter();
        } catch (DriverLoadException e) {
            
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
            
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage());
        } catch (ParseException e) {
            
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
        } catch (SemanticException e) {
            
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"semantic_expresion_error")+"\n"+e.getMessage());
        } catch (IOException e) {
            
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"input_output_error")+"\n"+e.getMessage());
        } catch (EvaluationException e) {
            
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage());
        } catch (com.hardcode.gdbms.parser.TokenMgrError e) {
            
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"expresion_error")+"\n"+e.getMessage());
        }

        return null;
    }






Matthieu Voorons escribió:
> Hi list,
> I'm trying to develop a new extension for gvsig. I wish to open the 
> dbf file related to a shapefile, modify it (add some fields and change 
> some entries) and make some sql queries. How could I do that ?
> I've seen that there is a dbf driver in the package 
> com.iver.cit.gvsig.fmap.drivers.dbf, and a dbf reader in the package 
> com.iver.cit.gvsig.fmap.drivers.shp. Which one should I use ?
> I've played a little with the code, the DbaseFileNIO and 
> DbaseFileHeaderNIO.
> Reading the header is not a problem, but I'm not able to acces the 
> data when using DbaseFileReaderNIO :
>
>        String filename = "C:\\properties.dbf";
>        File f = new File(filename);
>        try {
>            FileChannel in = new FileInputStream(filename).getChannel();
>            DbaseFileReaderNIO r = new DbaseFileReaderNIO(in);
>            int fields = r.getHeader().getNumFields();
>            int recordCount = r.getHeader().getNumRecords();
>            System.out.println("fields: " + fields + " recordCount: " + 
> recordCount);// This return 0 and 0 even if there are 
> 10                 
>                                                                        
>                                  // fields and 20 records
>            String fieldUniqueValueArr[] = new String[recordCount];
>            int j = 0;
>            while (r.hasNext()) {
>                DbaseFileReaderNIO.Row row = r.readRow();
>                System.out.println(row.read(0).toString());
>                fieldUniqueValueArr[j] = row.read(0).toString();
>                j++;
>           }
>        } catch (IOException ex) {
>            ex.printStackTrace();
>        }
>
> I'm also wondering how to make a SQL query on the dbf data.
> I would be very grateful if you could send me some code snippets or 
> some hints to where to digg.
>
> Best regards,
> Matthieu Voorons
> _______________________________________________
> Gvsig_internacional mailing list
> Gvsig_internacional at runas.cap.gva.es
> http://runas.cap.gva.es/mailman/listinfo/gvsig_internacional
>
>
>
Un saludo.

-- 
Vicente Caballero Navarro
Grupo desarrollo gvSIG.
IVER TI S.A.
C/ Salamanca, 50-52
46005-Valencia
Tlf.+34963163400
www.iver.es
www.gvsig.com




More information about the Gvsig_internacional mailing list