XML files

From Apache OpenOffice Wiki
Revision as of 06:47, 3 July 2006 by DrewJensen (Talk | contribs)

Jump to: navigation, search

The xml files will be simple, because if for no other reason I am not all that a proficint XMLer.

Delivery Route XSD

Before XML

Well, not wanting to wait on doing first things first - like creating xml data schemas...how about a throw back to the day. Meaning that for the moment I think we want to create some text files that will load our truck database. I have gone ahead and created an admin.odb file, with a basic structure the same as the driver.odb database. In this I will just add text tables that are identical to the tables to be exported to each driver.odb file. These would be Customers, Categories, Products, Routes and Employees.

With the HSQL engine exporting a table to a text file could not be easier. These couple of basic lines wll do this just fine.

Sub Main
    BasicLibraries.LoadLibrary("Drews")
    ' the library just supplies the getConnection routine
    exportTables()
End Sub

sub exportTables
dim conn

    conn = getConnection( "admin" )
    buildTextTable( Conn, "Categories" )
    buildTextTable( Conn, "Products" )
    buildTextTable( Conn, "Employees" )
    buildTextTable( Conn, "Customers" )
    conn.dispose

end sub

sub buildTextTable( aConn as variant, aTblName as variant  )
dim aTbl as variant
dim stmnt
dim strTableName as string
dim strCmd as string

    aTbl = aConn.Tables.GetByName( aTblName )
    stmnt = aConn.createStatement
    strTableName = "txt_" & aTbl.Name
    if aConn.Tables.HasByName( strTableName ) then
        aConn.Tables.DropByname( strTableName )
    end if
    stmnt.executeUpdate( "DROP TABLE " & strTableName & " IF EXISTS")
    stmnt.Escapeprocessing = False
    stmnt.execute( "SELECT * INTO TEXT """ & strTableName _
                    & """ FROM """ & aTbl.Name & """" )

end sub

Well, almost just fine. When the routine is done there are four updated files in the same directory as the admin.odb file: txt_categories.csv, txt_customers.csv, txt_products.csv and txt_employees.csv.

There is a subtle bug in this code...or in Base? If the file admin.odb is not open when this libraries main function is run it runs with out a problem, as many times as you care to run it.

However - if admin.odb is open ( this is for an embedded database at least ) then the following happens. The first time you run this code it runs fine. You get the text tables and the text files all created. Now the second time you run it an error will occur - stating that the insert into routine failed, because the text table already exists. Here is how to fix it - after the routine is run you must refresh the table view in the Base main window. If you don't do this the line aConn.Tables.HasByName fails to find the file. I suppose filing an issue is in order, mean time I will look for the proper command to use in a dispatch statment to refresh the table list, before we start as a work around.

That done, we can create our export files just that easily.

Next the mirror of these - the import routines for the driver.odb file.

Personal tools