Difference between revisions of "NL/Documentation/BASIC Guide/Data Sources"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Created page with "{{NL/Documentation/BASICGuideTOC/v2 |ShowPrevNext=block |ShowPrevPage=block |PrevPage=NL/Documentation/BASIC Guide/Database Access Types |NextPage=NL/Documentation/BASIC Guide..." (tussenstap opslaan))
(No difference)

Revision as of 18:09, 11 March 2013

Book.png


Een database kan worden opgenomen in Apache OpenOffice door een, waar in het algemeen naar wordt verwezen als een gegevensbron, te maken. De gebruikers-interface verschaft een overeenkomende optie voor het maken van Gegevensbronnen in het menu Extra. U kunt echter ook gegevensbronnen maken en daarmee werken met behulp van Apache OpenOffice BASIC.

Een database context-object dat is gemaakt met behulp van de functie createUnoService dient als het startpunt voor toegang tot een gegevensbron. Dit is gebaseerd op de service com.sun.star.sdb.DatabaseContext en is het basisobject voor alle bewerkingen van de database.

Het volgende voorbeeld toont hoe een databasecontext kan worden gemaakt en dan worden gebruikt om te bepalen of alle gegevensbronnen beschikbaar zijn. Het geeft de namen weer in een berichten-venster.


Dim DatabaseContext As Object
Dim Namen
Dim I As Integer
 
DatabaseContext = createUnoService("com.sun.star.sdb.DatabaseContext")
 
Namen = DatabaseContext.getElementNames()
 
For I = 0 To UBound(Namen())
  MsgBox Namen(I)
Next I

De individuele gegevensbronnen zijn gebaseerd op de service com.sun.star.sdb.DataSource en kunnen worden bepaald vanuit de databasecontext met behulp van de methode getByName:

Dim DatabaseContext As Object
Dim GegevensBron As Object
 
DatabaseContext = createUnoService("com.sun.star.sdb.DatabaseContext")
GegevensBron = DatabaseContext.getByName("Klanten")

Het voorbeeld maakt een object DataSource voor een gegevensbron, genaamd Klanten.

Data sources provide a range of properties, which in turn provide general information about the origin of the data and information about access methods. The properties are:

Name (String)
name of data source
URL (String)
URL of data source in the form of jdbc: subprotocol : subname or sdbc: subprotocol : subname
Settings (Array)
array containing PropertyValue-pairs with connection parameters (usually at least user name and password)
User (String)
user name
Password (String)
user password (is not saved)
IsPasswordRequired (Boolean)
the password is needed and is interactively requested from user.
IsReadOnly (Boolean)
permits read-only access to the database
NumberFormatsSupplier (Object)
object containing the number formats available for the database (supports the com.sun.star.util.XNumberFormatsSupplier interface)
TableFilter (Array)
list of table names to be displayed
TableTypeFilter (Array)
list of table types to be displayed. Values available are TABLE, VIEW and SYSTEM TABLE
SuppressVersionColumns (Boolean)
suppresses the display of columns that are used for version administration

Template:Documentation/Note

Queries

Predefined queries can be assigned to a data source. Apache OpenOffice notes the SQL commands of queries so that they are available at all times. Queries are used to simplify working with databases because they can be opened with a simple mouse click and also provide users without any knowledge of SQL with the option of issuing SQL commands.

An object which supports the com.sun.star.sdb.QueryDefinition service is concealed behind a query. The queries are accessed by means of the QueryDefinitions method of the data source.

The following example lists the names of data source queries can be established in a message box.

Dim DatabaseContext As Object
Dim DataSource As Object
Dim QueryDefinitions As Object
Dim QueryDefinition As Object
Dim I As Integer
 
DatabaseContext = createUnoService("com.sun.star.sdb.DatabaseContext")
DataSource = DatabaseContext.getByName("Customers")
QueryDefinitions = DataSource.getQueryDefinitions()
 
For I = 0 To QueryDefinitions.Count() - 1
  QueryDefinition = QueryDefinitions(I)
  MsgBox QueryDefinition.Name
Next I

In addition to the Name property used in the example, the com.sun.star.sdb.QueryDefinition provides a whole range of other properties. These are:

Name (String)
query name
Command (String)
SQL command (typically a SELECT command)

The following example shows how a query object can be created in a program-controlled manner and can be assigned to a data source.

Dim DatabaseContext As Object
Dim DataSource As Object
Dim QueryDefinitions As Object
Dim QueryDefinition As Object
Dim I As Integer
 
DatabaseContext = createUnoService("com.sun.star.sdb.DatabaseContext")
DataSource = DatabaseContext.getByName("Customers")
QueryDefinitions = DataSource.getQueryDefinitions()
QueryDefinition = createUnoService("com.sun.star.sdb.QueryDefinition")
QueryDefinition.Command = "SELECT * FROM Customer"
QueryDefinitions.insertByName("NewQuery", QueryDefinition)

The query object is first created using the createUnoService call, then initialized, and then inserted into the QueryDefinitions object by means of insertByName.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools