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.

Data Sources<br />

Command (String)<br />

SQL command (typically a SELECT command)<br />

The following example shows how a query object can be created in a program-controlled manner and can be<br />

assigned to a data source.<br />

Dim DatabaseContext As Object<br />

Dim DataSource As Object<br />

Dim QueryDefinitions As Object<br />

Dim QueryDefinition As Object<br />

Dim I As Integer<br />

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

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

QueryDefinitions = DataSource.getQueryDefinitions()<br />

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

QueryDefinition.Command = "SELECT * FROM Customer"<br />

QueryDefinitions.insertByName("NewQuery", QueryDefinition)<br />

The query object is first created using the createUnoService call, then initialized, and then inserted into the<br />

QueryDefinitions object by means of insertByName.<br />

Database Access<br />

A database connection is needed for access to a database. This is a transfer channel which permits direct<br />

communication with the database. Unlike the data sources presented in the previous section, the database<br />

connection must therefore be re-established every time the program is restarted.<br />

<strong>OpenOffice</strong>.<strong>org</strong> provides various ways of establishing database connections. This example shows how to connect<br />

to an existing data source.<br />

Dim DatabaseContext As Object<br />

Dim DataSource As Object<br />

Dim Connection As Object<br />

Dim InteractionHandler 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 />

The code used in the example first checks whether the database is password protected. If not, it creates the<br />

database connection required using the GetConnection call. The two empty strings in the command line stand<br />

for the user name and password.<br />

If the database is password protected, the example creates an InteractionHandler and opens the database<br />

connection using the ConnectWithCompletion method. The InteractionHandler ensures that <strong>OpenOffice</strong>.<strong>org</strong><br />

asks the user for the required login data.<br />

Iteration of Tables<br />

A table is usually accessed in <strong>OpenOffice</strong>.<strong>org</strong> through the ResultSet object. A ResultSet is a type of marker<br />

that indicates a current set of data within a volume of results obtained using the SELECT command.<br />

This example shows how a ResultSet can be used to query values from a database table.<br />

Dim DatabaseContext As Object<br />

Dim DataSource As Object<br />

Dim Connection As Object<br />

Dim InteractionHandler as Object<br />

146 <strong>OpenOffice</strong>.<strong>org</strong> 3.2 <strong>BASIC</strong> <strong>Guide</strong> · March 2010

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

Saved successfully!

Ooh no, something went wrong!