21.08.2013 Views

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Dim Statement As Object<br />

Dim ResultSet As Object<br />

DatabaseContext = createUnoService("com.sun.star.sdb.DatabaseContext")<br />

DataSource = DatabaseContext.getByName("Customers")<br />

If Not DataSource.IsPasswordRequired Then<br />

Connection = DataSource.GetConnection("","")<br />

Else<br />

InteractionHandler = createUnoService("com.sun.star.sdb.InteractionHandler")<br />

Connection = DataSource.ConnectWithCompletion(InteractionHandler)<br />

End If<br />

Statement = Connection.createStatement()<br />

ResultSet = Statement.executeQuery("SELECT ""CustomerNumber"" FROM ""Customer""")<br />

If Not IsNull(ResultSet) Then<br />

While ResultSet.next<br />

MsgBox ResultSet.getString(1)<br />

Wend<br />

End If<br />

Database Access<br />

Once the database connection has been established, the code used in the example first uses the<br />

Connection.createObject call to create a Statement object. This Statement object then uses the<br />

executeQuery call to return the actual ResultSet. The program now checks whether the ResultSet actually<br />

exists and traverses the data records using a loop. The values required (in the example, those from the<br />

CustomerNumber field) returns the ResultSet using the getString method, whereby the parameter 1<br />

determines that the call relates to the values of the first column.<br />

Note – VBA : The ResultSet object from SDBC is comparable with the Recordset object from DAO and<br />

ADO, since this also provides iterative access to a database.<br />

Note – StarOffice 5 : The database is actually accessed in <strong>OpenOffice</strong>.<strong>org</strong> through a ResultSet object. This<br />

reflects the content of a table or the result of a SQL-SELECT command. In the past, the ResultSet object<br />

provided the resident methods in the Application object for navigation within the data, for example,<br />

DataNextRecord ).<br />

Type-Specific Methods for Retrieving Values<br />

As can be seen in the example from the previous section, <strong>OpenOffice</strong>.<strong>org</strong> provides a getString method for<br />

accessing table contents. The method provides the result in the form of a string. The following get methods are<br />

available:<br />

getByte()<br />

supports the SQL data types for numbers, characters and strings<br />

getShort()<br />

getInt()<br />

getLong()<br />

supports the SQL data types for numbers, characters and strings<br />

supports the SQL data types for numbers, characters and strings<br />

supports the SQL data types for numbers, characters and strings<br />

getFloat()<br />

supports the SQL data types for numbers, characters and strings<br />

getDouble()<br />

supports the SQL data types for numbers, characters and strings<br />

getBoolean()<br />

supports the SQL data types for numbers, characters and strings<br />

Chapter 10 · Databases 147

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!