Difference between revisions of "NL/Documentation/BASIC Guide/Database Access"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Created page with "{{NL/Documentation/BASICGuideTOC/v2 |ShowPrevNext=block |ShowPrevPage=block |PrevPage=NL/Documentation/BASIC Guide/Data Sources |NextPage=NL/Documentation/BASIC Guide/Dialogs ..." (tussenstap opslaan))
 
Line 66: Line 66:
 
</source>
 
</source>
  
Once the database connection has been established, the code used in the example first uses the <tt>Connection.createObject</tt> call to create a <tt>Statement</tt> object. This <tt>Statement</tt> object then uses the <tt>executeQuery</tt> call to return the actual <tt>ResultSet</tt>. The program now checks whether the <tt>ResultSet</tt> actually exists and traverses the data records using a loop. The values required (in the example, those from the <tt>CustomerNumber</tt> field) returns the <tt>ResultSet</tt> using the <tt>getString</tt> method, whereby the parameter 1 determines that the call relates to the values of the first column.
+
Als de verbinding met de gegevensbron eenmaal is gemaakt, gebruikt de in het voorbeeld gebruikte code eerst de aanroep <tt>Verbinding.createObject</tt> om een object <tt>Argument</tt> te maken. Dit object <tt>Argument</tt> gebruikt dan de  aanroep <tt>executeQuery</tt> om de actuele <tt>ResultatenSet</tt> weer te geven. Het programma controleert nu of de <tt>ResultatenSet</tt> 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 <tt>KlantNummer</tt>-) geven de <tt>ResultatenSet</tt> weer door middel van de methode <tt>getString</tt>, waarbij de parameter 1 bepaalt dat de aanroep gerelateerd is aan de waarden uit de eerste kolom.
  
{{Documentation/VBAnote|The <tt>ResultSet</tt> object from SDBC is comparable with the <tt>Recordset</tt> object from DAO and ADO, since this also provides iterative access to a database.}}
+
{{Documentation/VBAnote|Het object <tt>ResultSet</tt> uit SDBC is te vergelijken met het object <tt>Recordset</tt> in DAO en ADO, omdat dit ook iteratieve toegang verschaft tot een database.}}
  
{{Documentation/SO5note|The database is actually accessed in {{OOo}} through a <tt>ResultSet</tt> object. This reflects the content of a table or the result of a SQL-SELECT command. In the past, the <tt>ResultSet</tt> object provided the resident methods in the <tt>Application</tt> object for navigation within the data, for example, <tt>DataNextRecord</tt> ).}}
+
{{Documentation/SO5note|Toegang tot de database wordt actueel in {{OOo}} verkregen door een object <tt>ResultSet</tt>. Dit reflecteert de inhoud van een tabel of het resultaat van een opdracht SELECT in SQL. In het verleden verschafte het object <tt>ResultSet</tt> de residente methoden en het object <tt>Application</tt> om te navigeren binnen de gegevens, bijvoorbeeld: <tt>DataNextRecord</tt> ).}}
  
== Type-Specific Methods for Retrieving Values ==
+
== Type-specifieke methoden voor het ophalen van waarden ==
  
 
As can be seen in the example from the previous section, {{OOo}} provides a <tt>getString</tt> method for accessing table contents. The method provides the result in the form of a string. The following <tt>get</tt> methods are available:
 
As can be seen in the example from the previous section, {{OOo}} provides a <tt>getString</tt> method for accessing table contents. The method provides the result in the form of a string. The following <tt>get</tt> methods are available:

Revision as of 18:56, 11 March 2013

Book.png


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.

Documentation note.png 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.


Documentation note.png StarOffice 5 : Toegang tot de database wordt actueel in Apache OpenOffice verkregen door een object ResultSet. Dit reflecteert de inhoud van een tabel of het resultaat van een opdracht SELECT in SQL. In het verleden verschafte het object ResultSet de residente methoden en het object Application om te navigeren binnen de gegevens, bijvoorbeeld: DataNextRecord ).


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
Documentation note.png 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.

Methods for Navigation in ResultSets

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).
Personal tools