Toegang tot database
- SQL: een Query-taal
- Typen toegang tot database
- Gegevensbronnen
- Toegang tot database
Een verbinding met een gegevensbron is nodig voor toegang tot de gegevensbron. Dit is een overdrachtskanaal dat directe communicatie met de gegevensbron toestaat. Anders dan de in het voorgaande gedeelte gepresenteerde gegevensbron, moet de verbinding naar de gegevensbron daarom elke keer opnieuw worden gemaakt, iedere keer als het programma opnieuw wordt gestart.
Apache OpenOffice verschaft verschillende manieren voor het maken van verbindingen met een gegevensbron. Dit voorbeeld geeft weer hoe een verbinding met een bestaande gegevensbron kan worden gemaakt
Dim DatabaseContext As Object Dim GegevensBron As Object Dim Verbinding As Object Dim InteractieHandelaar as Object DatabaseContext = createUnoService("com.sun.star.sdb.DatabaseContext") GegevensBron = DatabaseContext.getByName("Customers") If Not GegevensBron.IsPasswordRequired Then Verbinding = GegevensBron.GetConnection("","") Else InteractieHandelaar = createUnoService("com.sun.star.sdb.InteractionHandler") Verbinding = GegevensBron.ConnectWithCompletion(InteractieHandelaar) End If
De in het voorbeeld gebruikte code controleert eerst of de gegevensbron met een wachtwoord beveiligd is. Als dat niet het geval is maakt het de verbinding met de gegevensbron met behulp van de aanroep GetConnection. De twee lege tekenreeksen in de commandoregel staan voor de gebruikersnaam en het wachtwoord. Als de gegevensbron wachtwoord-beveiligd is, maakt het voorbeeld een InteractieHandelaar en opent de verbinding net de gegevensbron met behulp van de methode ConnectWithCompletion. De InteractieHandelaar zorgt ervoor dat Apache OpenOffice de gebruiker vraagt naar de vereiste gegevens voor toegang (login).
Iteratie van tabellen
Een tabel wordt normaal gesproken in Apache OpenOffice benaderd via het object ResultSet. Een ResultSet is een type van aanduiding dat een huidige reeks van gegevens aangeeft binnen een reeks met resultaten welke is verkregen met met behulp van de opdracht SELECT.
Dit voorbeeld toont hoe een ResultSet kan worden gebruikt om waarden uit een tabel van de gegevensbron te bevragen.
Dim DatabaseContext As Object Dim GegevensBron As Object Dim Verbinding As Object Dim InteractieHandelaar as Object Dim Argument As Object Dim ResultatenSet As Object DatabaseContext = createUnoService("com.sun.star.sdb.DatabaseContext") GegevensBron = DatabaseContext.getByName("Klanten") If Not DataSource.IsPasswordRequired Then Connection = GegevensBron.GetConnection("","") Else InteractieHandelaar = createUnoService("com.sun.star.sdb.InteractionHandler") Connection = GegevensBron.ConnectWithCompletion(InteractieHandelaar) End If Argument = Verbinding.createStatement() ResultatenSet = Argument.executeQuery("SELECT ""KlantNummer"" FROM ""Klanten""") If Not IsNull(ResultatenSet) Then While ResultatenSet.next MsgBox ResultatenSet.getString(1) Wend End If
Als de verbinding met de gegevensbron eenmaal is gemaakt, gebruikt de in het voorbeeld gebruikte code eerst de aanroep Verbinding.createObject om een object Argument te maken. Dit object Argument gebruikt dan de aanroep executeQuery om de actuele ResultatenSet weer te geven. Het programma controleert nu of de ResultatenSet daadwerkelijk bestaat en gaat door de records van de gegevensbron door middel van een lus. De vereiste waarden (in het voorbeeld die vanuit het veld KlantNummer-) geven de ResultatenSet weer door middel van de methode getString, waarbij de parameter 1 bepaalt dat de aanroep gerelateerd is aan de waarden uit de eerste kolom.
VBA : Het object ResultSet uit SDBC is te vergelijken met het object Recordset in DAO en ADO, omdat dit ook iteratieve toegang verschaft tot een database. |
Type-specifieke methoden voor het ophalen van waarden
As can be seen in the example from the previous section, Apache OpenOffice provides a getString method for accessing table contents. The method provides the result in the form of a string. The following get methods are available:
- getByte()
- supports the SQL data types for numbers, characters and strings
- getShort()
- supports the SQL data types for numbers, characters and strings
- getInt()
- supports the SQL data types for numbers, characters and strings
- getLong()
- supports the SQL data types for numbers, characters and strings
- getFloat()
- supports the SQL data types for numbers, characters and strings
- getDouble()
- supports the SQL data types for numbers, characters and strings
- getBoolean()
- supports the SQL data types for numbers, characters and strings
- getString()
- supports all SQL data types
- getBytes()
- supports the SQL data types for binary values
- getDate()
- supports the SQL data types for numbers, strings, date and time stamp
- getTime()
- supports the SQL data types for numbers, strings, date and time stamp
- getTimestamp()
- supports the SQL data types for numbers, strings, date and time stamp
- getCharacterStream()
- supports the SQL data types for numbers, strings and binary values
- getUnicodeStream()
- supports the SQL data types for numbers, strings and binary values
- getBinaryStream()
- binary values
- getObject()
- supports all SQL data types
In all instances, the number of columns should be listed as a parameter whose values should be queried.
The ResultSet Variants
Accessing databases is often a matter of critical speed. Apache OpenOffice provides several ways of optimizing ResultSets and thereby controlling the speed of access. The more functions a ResultSet provides, the more complex its implementation usually is and therefore the slower the functions are.
A simple ResultSet, provides the minimum scope of functions available. It only allows iteration to be applied forward, and for values to be interrogated. More extensive navigation options, such as the possibility of modifying values, are therefore not included.
The Statement object used to create the ResultSet provides some properties which allow the functions of the ResultSet to be influenced:
- ResultSetConcurrency (const)
- specifications as to whether the data can be modified (specifications in accordance with com.sun.star.sdbc.ResultSetConcurrency).
- ResultSetType (const)
- specifications regarding type of ResultSets ( specifications in accordance with com.sun.star.sdbc.ResultSetType).
The values defined in com.sun.star.sdbc.ResultSetConcurrency are:
- UPDATABLE
- ResultSet permits values to be modified
- READ_ONLY
- ResultSet does not permit modifications
The com.sun.star.sdbc.ResultSetConcurrency group of constants provides the following specifications:
- FORWARD_ONLY
- ResultSet only permits forward navigation
- SCROLL_INSENSITIVE
- ResultSet permits any type of navigation, changes to the original data are, however, not noted
- SCROLL_SENSITIVE
- ResultSet permits any type of navigation, changes to the original data impact on the ResultSet
VBA : A ResultSet containing the READ_ONLY and SCROLL_INSENSITIVE properties corresponds to a record set of the Snapshot type in ADO and DAO. |
When using the ResultSet's UPDATEABLE and SCROLL_SENSITIVE properties, the scope of function of a ResultSet is comparable with a Dynaset type Recordset from ADO and DAO.
If a ResultSet is a SCROLL_INSENSITIVE or SCROLL_SENSITIVE type, it supports a whole range of methods for navigation in the stock of data. The central methods are:
- next()
- navigation to the next data record
- previous()
- navigation to the previous data record
- first()
- navigation to the first data record
- last()
- navigation to the last data record
- beforeFirst()
- navigation to before the first data record
- afterLast()
- navigation to after the last data record
All methods return a Boolean parameter which specifies whether the navigation was successful.
To determine the current cursor position, the following test methods are provided and all return a Boolean value:
- isBeforeFirst()
- ResultSet is before the first data record
- isAfterLast()
- ResultSet is after the last data record
- isFirst()
- ResultSet is the first data record
- isLast()
- ResultSet is the last data record
Modifying Data Records
If a ResultSet has been created with the ResultSetConcurrency = UPDATEABLE value, then its content can be edited. This only applies for as long as the SQL command allows the data to be re-written to the database (depends on principle). This is not, for example, possible with complex SQL commands with linked columns or accumulated values.
The ResultSet object provides Update methods for modifying values, which are structured in the same way as the get methods for retrieving values. The updateString method, for example, allows a string to be written.
After modification, the values must be transferred into the database using the updateRow()method. The call must take place before the next navigation command, otherwise the values will be lost.
If an error is made during the modifications, this can be undone using the cancelRowUpdates()method. This call is only available provided that the data has not be re-written into the database using updateRow().
Content on this page is licensed under the Public Documentation License (PDL). |