<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.harlamenkov.ru/index.php?action=history&amp;feed=atom&amp;title=EN%2Fdocumentation%2Fbasic_guide%2Fdatabase_access</id>
	<title>EN/documentation/basic guide/database access - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.harlamenkov.ru/index.php?action=history&amp;feed=atom&amp;title=EN%2Fdocumentation%2Fbasic_guide%2Fdatabase_access"/>
	<link rel="alternate" type="text/html" href="https://wiki.harlamenkov.ru/index.php?title=EN/documentation/basic_guide/database_access&amp;action=history"/>
	<updated>2026-11-11T13:50:03Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.33.0</generator>
	<entry>
		<id>https://wiki.harlamenkov.ru/index.php?title=EN/documentation/basic_guide/database_access&amp;diff=16632&amp;oldid=prev</id>
		<title>Sancho: Основной текст</title>
		<link rel="alternate" type="text/html" href="https://wiki.harlamenkov.ru/index.php?title=EN/documentation/basic_guide/database_access&amp;diff=16632&amp;oldid=prev"/>
		<updated>2011-09-05T17:25:44Z</updated>

		<summary type="html">&lt;p&gt;Основной текст&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;section begin=title /&amp;gt;&amp;lt;noinclude&amp;gt;{{DISPLAYTITLE:&amp;lt;/noinclude&amp;gt;Database Access&amp;lt;noinclude&amp;gt;}}&amp;lt;/noinclude&amp;gt;&amp;lt;section end=title /&amp;gt;{{BreadCrumbL10n}}&lt;br /&gt;
&lt;br /&gt;
{{Documentation/BASICGuideTOC&lt;br /&gt;
|ShowPrevNext=block&lt;br /&gt;
|ShowPrevPage=block&lt;br /&gt;
|PrevPage=EN/documentation/basic_guide/data_sources&lt;br /&gt;
|NextPage=EN/documentation/basic_guide/dialogs&lt;br /&gt;
|base=block&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
__NOTOC__&lt;br /&gt;
&lt;br /&gt;
A database connection is needed for access to a database. This is a transfer channel which permits direct communication with the database. Unlike the data sources presented in the previous section, the database connection must therefore be re-established every time the program is restarted.&lt;br /&gt;
&lt;br /&gt;
{{OOo}} provides various ways of establishing database connections. This example shows how to connect to an existing data source.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Dim DatabaseContext As Object&lt;br /&gt;
Dim DataSource As Object&lt;br /&gt;
Dim Connection As Object&lt;br /&gt;
Dim InteractionHandler as Object&lt;br /&gt;
&lt;br /&gt;
DatabaseContext = createUnoService(&amp;quot;com.sun.star.sdb.DatabaseContext&amp;quot;)&lt;br /&gt;
DataSource = DatabaseContext.getByName(&amp;quot;Customers&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
If Not DataSource.IsPasswordRequired Then&lt;br /&gt;
  Connection = DataSource.GetConnection(&amp;quot;&amp;quot;,&amp;quot;&amp;quot;)&lt;br /&gt;
Else&lt;br /&gt;
  InteractionHandler = createUnoService(&amp;quot;com.sun.star.sdb.InteractionHandler&amp;quot;)&lt;br /&gt;
  Connection = DataSource.ConnectWithCompletion(InteractionHandler)&lt;br /&gt;
End If&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code used in the example first checks whether the database is password protected. If not, it creates the database connection required using the &amp;lt;tt&amp;gt;GetConnection&amp;lt;/tt&amp;gt; call. The two empty strings in the command line stand for the user name and password.&lt;br /&gt;
&lt;br /&gt;
If the database is password protected, the example creates an &amp;lt;tt&amp;gt;InteractionHandler&amp;lt;/tt&amp;gt; and opens the database connection using the &amp;lt;tt&amp;gt;ConnectWithCompletion&amp;lt;/tt&amp;gt; method. The InteractionHandler ensures that {{OOo}} asks the user for the required login data.&lt;br /&gt;
&lt;br /&gt;
== Iteration of Tables ==&lt;br /&gt;
A table is usually accessed in {{OOo}} through the &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; object. A &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; is a type of marker that indicates a current set of data within a volume of results obtained using the &amp;lt;tt&amp;gt;SELECT&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
&lt;br /&gt;
This example shows how a &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; can be used to query values from a database table.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Dim DatabaseContext As Object&lt;br /&gt;
Dim DataSource As Object&lt;br /&gt;
Dim Connection As Object&lt;br /&gt;
Dim InteractionHandler as Object&lt;br /&gt;
Dim Statement As Object&lt;br /&gt;
Dim ResultSet As Object&lt;br /&gt;
&lt;br /&gt;
DatabaseContext = createUnoService(&amp;quot;com.sun.star.sdb.DatabaseContext&amp;quot;)&lt;br /&gt;
DataSource = DatabaseContext.getByName(&amp;quot;Customers&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
If Not DataSource.IsPasswordRequired Then&lt;br /&gt;
  Connection = DataSource.GetConnection(&amp;quot;&amp;quot;,&amp;quot;&amp;quot;)&lt;br /&gt;
Else&lt;br /&gt;
  InteractionHandler = createUnoService(&amp;quot;com.sun.star.sdb.InteractionHandler&amp;quot;)&lt;br /&gt;
  Connection = DataSource.ConnectWithCompletion(InteractionHandler)&lt;br /&gt;
End If&lt;br /&gt;
&lt;br /&gt;
Statement = Connection.createStatement()&lt;br /&gt;
ResultSet = Statement.executeQuery(&amp;quot;SELECT &amp;quot;&amp;quot;CustomerNumber&amp;quot;&amp;quot; FROM &amp;quot;&amp;quot;Customer&amp;quot;&amp;quot;&amp;quot;) &lt;br /&gt;
&lt;br /&gt;
If Not IsNull(ResultSet) Then&lt;br /&gt;
  While ResultSet.next&lt;br /&gt;
    MsgBox ResultSet.getString(1)&lt;br /&gt;
  Wend&lt;br /&gt;
End If&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once the database connection has been established, the code used in the example first uses the &amp;lt;tt&amp;gt;Connection.createObject&amp;lt;/tt&amp;gt; call to create a &amp;lt;tt&amp;gt;Statement&amp;lt;/tt&amp;gt; object. This &amp;lt;tt&amp;gt;Statement&amp;lt;/tt&amp;gt; object then uses the &amp;lt;tt&amp;gt;executeQuery&amp;lt;/tt&amp;gt; call to return the actual &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt;. The program now checks whether the &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; actually exists and traverses the data records using a loop. The values required (in the example, those from the &amp;lt;tt&amp;gt;CustomerNumber&amp;lt;/tt&amp;gt; field) returns the &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; using the &amp;lt;tt&amp;gt;getString&amp;lt;/tt&amp;gt; method, whereby the parameter 1 determines that the call relates to the values of the first column.&lt;br /&gt;
&lt;br /&gt;
{{VBAnote|The &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; object from SDBC is comparable with the &amp;lt;tt&amp;gt;Recordset&amp;lt;/tt&amp;gt; object from DAO and ADO, since this also provides iterative access to a database.}}&lt;br /&gt;
&lt;br /&gt;
{{SO5note|The database is actually accessed in {{OOo}} through a &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; object. This reflects the content of a table or the result of a SQL-SELECT command. In the past, the &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; object provided the resident methods in the &amp;lt;tt&amp;gt;Application&amp;lt;/tt&amp;gt; object for navigation within the data, for example, &amp;lt;tt&amp;gt;DataNextRecord&amp;lt;/tt&amp;gt; ).}}&lt;br /&gt;
&lt;br /&gt;
== Type-Specific Methods for Retrieving Values ==&lt;br /&gt;
&lt;br /&gt;
As can be seen in the example from the previous section, {{OOo}} provides a &amp;lt;tt&amp;gt;getString&amp;lt;/tt&amp;gt; method for accessing table contents. The method provides the result in the form of a string. The following &amp;lt;tt&amp;gt;get&amp;lt;/tt&amp;gt; methods are available:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;getByte()&amp;lt;/tt&amp;gt;:supports the SQL data types for numbers, characters and strings&lt;br /&gt;
;&amp;lt;tt&amp;gt;getShort()&amp;lt;/tt&amp;gt;:supports the SQL data types for numbers, characters and strings&lt;br /&gt;
;&amp;lt;tt&amp;gt;getInt()&amp;lt;/tt&amp;gt;:supports the SQL data types for numbers, characters and strings&lt;br /&gt;
;&amp;lt;tt&amp;gt;getLong()&amp;lt;/tt&amp;gt;:supports the SQL data types for numbers, characters and strings&lt;br /&gt;
;&amp;lt;tt&amp;gt;getFloat()&amp;lt;/tt&amp;gt;:supports the SQL data types for numbers, characters and strings&lt;br /&gt;
;&amp;lt;tt&amp;gt;getDouble()&amp;lt;/tt&amp;gt;:supports the SQL data types for numbers, characters and strings&lt;br /&gt;
;&amp;lt;tt&amp;gt;getBoolean()&amp;lt;/tt&amp;gt;:supports the SQL data types for numbers, characters and strings&lt;br /&gt;
;&amp;lt;tt&amp;gt;getString()&amp;lt;/tt&amp;gt;:supports all SQL data types&lt;br /&gt;
;&amp;lt;tt&amp;gt;getBytes()&amp;lt;/tt&amp;gt;:supports the SQL data types for binary values&lt;br /&gt;
;&amp;lt;tt&amp;gt;getDate()&amp;lt;/tt&amp;gt;:supports the SQL data types for numbers, strings, date and time stamp&lt;br /&gt;
;&amp;lt;tt&amp;gt;getTime()&amp;lt;/tt&amp;gt;:supports the SQL data types for numbers, strings, date and time stamp&lt;br /&gt;
;&amp;lt;tt&amp;gt;getTimestamp()&amp;lt;/tt&amp;gt;:supports the SQL data types for numbers, strings, date and time stamp&lt;br /&gt;
;&amp;lt;tt&amp;gt;getCharacterStream()&amp;lt;/tt&amp;gt;:supports the SQL data types for numbers, strings and binary values&lt;br /&gt;
;&amp;lt;tt&amp;gt;getUnicodeStream()&amp;lt;/tt&amp;gt;:supports the SQL data types for numbers, strings and binary values&lt;br /&gt;
;&amp;lt;tt&amp;gt;getBinaryStream()&amp;lt;/tt&amp;gt;:binary values&lt;br /&gt;
;&amp;lt;tt&amp;gt;getObject()&amp;lt;/tt&amp;gt;:supports all SQL data types&lt;br /&gt;
&lt;br /&gt;
In all instances, the number of columns should be listed as a parameter whose values should be queried.&lt;br /&gt;
&lt;br /&gt;
== The ResultSet Variants ==&lt;br /&gt;
&lt;br /&gt;
Accessing databases is often a matter of critical speed. {{OOo}} provides several ways of optimizing &amp;lt;tt&amp;gt;ResultSets&amp;lt;/tt&amp;gt; and thereby controlling the speed of access. The more functions a &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; provides, the more complex its implementation usually is and therefore the slower the functions are.&lt;br /&gt;
&lt;br /&gt;
A simple &amp;lt;tt&amp;gt;ResultSet,&amp;lt;/tt&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
The Statement object used to create the &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; provides some properties which allow the functions of the &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; to be influenced:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;ResultSetConcurrency (const)&amp;lt;/tt&amp;gt;:specifications as to whether the data can be modified (specifications in accordance with &amp;lt;idl&amp;gt;com.sun.star.sdbc.ResultSetConcurrency&amp;lt;/idl&amp;gt;).&lt;br /&gt;
;&amp;lt;tt&amp;gt;ResultSetType (const)&amp;lt;/tt&amp;gt;:specifications regarding type of &amp;lt;tt&amp;gt;ResultSets&amp;lt;/tt&amp;gt; ( specifications in accordance with &amp;lt;idl&amp;gt;com.sun.star.sdbc.ResultSetType&amp;lt;/idl&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
The values defined in &amp;lt;idl&amp;gt;com.sun.star.sdbc.ResultSetConcurrency&amp;lt;/idl&amp;gt; are:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;UPDATABLE&amp;lt;/tt&amp;gt;:&amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; permits values to be modified&lt;br /&gt;
;&amp;lt;tt&amp;gt;READ_ONLY&amp;lt;/tt&amp;gt;:&amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; does not permit modifications&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;idl&amp;gt;com.sun.star.sdbc.ResultSetConcurrency&amp;lt;/idl&amp;gt; group of constants provides the following specifications:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;FORWARD_ONLY&amp;lt;/tt&amp;gt;:&amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; only permits forward navigation&lt;br /&gt;
;&amp;lt;tt&amp;gt;SCROLL_INSENSITIVE&amp;lt;/tt&amp;gt;:&amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; permits any type of navigation, changes to the original data are, however, not noted&lt;br /&gt;
;&amp;lt;tt&amp;gt;SCROLL_SENSITIVE&amp;lt;/tt&amp;gt;:&amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; permits any type of navigation, changes to the original data impact on the &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{VBAnote|A &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; containing the &amp;lt;tt&amp;gt;READ_ONLY&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SCROLL_INSENSITIVE&amp;lt;/tt&amp;gt; properties corresponds to a record set of the &amp;lt;tt&amp;gt;Snapshot&amp;lt;/tt&amp;gt; type in ADO and DAO. }}&lt;br /&gt;
&lt;br /&gt;
When using the &amp;lt;tt&amp;gt;ResultSet's&amp;lt;/tt&amp;gt; &amp;lt;tt&amp;gt;UPDATEABLE&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;SCROLL_SENSITIVE&amp;lt;/tt&amp;gt; properties, the scope of function of a &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; is comparable with a &amp;lt;tt&amp;gt;Dynaset&amp;lt;/tt&amp;gt; type &amp;lt;tt&amp;gt;Recordset&amp;lt;/tt&amp;gt; from ADO and DAO.&lt;br /&gt;
&lt;br /&gt;
== Methods for Navigation in ResultSets ==&lt;br /&gt;
&lt;br /&gt;
If a &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; is a &amp;lt;tt&amp;gt;SCROLL_INSENSITIVE&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;SCROLL_SENSITIVE&amp;lt;/tt&amp;gt; type, it supports a whole range of methods for navigation in the stock of data. The central methods are:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;next()&amp;lt;/tt&amp;gt;:navigation to the next data record&lt;br /&gt;
;&amp;lt;tt&amp;gt;previous()&amp;lt;/tt&amp;gt;:navigation to the previous data record&lt;br /&gt;
;&amp;lt;tt&amp;gt;first()&amp;lt;/tt&amp;gt;:navigation to the first data record&lt;br /&gt;
;&amp;lt;tt&amp;gt;last()&amp;lt;/tt&amp;gt;:navigation to the last data record&lt;br /&gt;
;&amp;lt;tt&amp;gt;beforeFirst()&amp;lt;/tt&amp;gt;:navigation to before the first data record&lt;br /&gt;
;&amp;lt;tt&amp;gt;afterLast()&amp;lt;/tt&amp;gt;:navigation to after the last data record&lt;br /&gt;
&lt;br /&gt;
All methods return a Boolean parameter which specifies whether the navigation was successful.&lt;br /&gt;
&lt;br /&gt;
To determine the current cursor position, the following test methods are provided and all return a Boolean value:&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;isBeforeFirst()&amp;lt;/tt&amp;gt;:&amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; is before the first data record&lt;br /&gt;
;&amp;lt;tt&amp;gt;isAfterLast()&amp;lt;/tt&amp;gt;:&amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; is after the last data record&lt;br /&gt;
;&amp;lt;tt&amp;gt;isFirst()&amp;lt;/tt&amp;gt;:&amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; is the first data record&lt;br /&gt;
;&amp;lt;tt&amp;gt;isLast()&amp;lt;/tt&amp;gt;:&amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; is the last data record&lt;br /&gt;
&lt;br /&gt;
== Modifying Data Records ==&lt;br /&gt;
&lt;br /&gt;
If a &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; has been created with the &amp;lt;tt&amp;gt;ResultSetConcurrency&amp;lt;/tt&amp;gt; = &amp;lt;tt&amp;gt;UPDATEABLE&amp;lt;/tt&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;ResultSet&amp;lt;/tt&amp;gt; object provides &amp;lt;tt&amp;gt;Update&amp;lt;/tt&amp;gt; methods for modifying values, which are structured in the same way as the &amp;lt;tt&amp;gt;get&amp;lt;/tt&amp;gt; methods for retrieving values. The &amp;lt;tt&amp;gt;updateString&amp;lt;/tt&amp;gt; method, for example, allows a string to be written.&lt;br /&gt;
&lt;br /&gt;
After modification, the values must be transferred into the database using the &amp;lt;tt&amp;gt;updateRow()&amp;lt;/tt&amp;gt;method. The call must take place before the next navigation command, otherwise the values will be lost.&lt;br /&gt;
&lt;br /&gt;
If an error is made during the modifications, this can be undone using the &amp;lt;tt&amp;gt;cancelRowUpdates()&amp;lt;/tt&amp;gt;method. This call is only available provided that the data has not be re-written into the database using &amp;lt;tt&amp;gt;updateRow()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
{{PDL1}}&lt;/div&gt;</summary>
		<author><name>Sancho</name></author>
		
	</entry>
</feed>