Macro to execute a simple SQL query in OObase

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
dargosch
Posts: 48
Joined: Wed Jun 11, 2008 1:28 pm

Macro to execute a simple SQL query in OObase

Post by dargosch »

Hi,

I have tree querys I need to execute on the push of a putton in a form in OObase. So, I need a Macro for that.. Is there simple tutorial on how to do this? Just get the query to execute. Nothing will be returned from the query.

/Fredrik
--
"Life is like a trumpet - if you don't put anything into it, you don't get anything out of it."
OOo 3.0.X on Mac OSx Leopard
User avatar
probe1
Volunteer
Posts: 277
Joined: Mon Oct 08, 2007 1:34 am
Location: Chonburi Thailand

Re: Macro to execute a simple SQL query in OObase

Post by probe1 »

Frederik,

see this example. It retrieves a query "NameOfQuery" from the OOo registered data source "NameRegDB" and executes it.

Code: Select all

Sub xQuery
' Kontext holen
oDatenbankKontext = CreateUnoService ( "com.sun.star.sdb.DataBaseContext" )
' Datenquelle auswählen
oDatenquelle = oDatenbankKontext.getByName( "NameRegDB" )


sQuery = "NameOfQuery"

' Abfragen
oAbfragen = oDatenquelle.QueryDefinitions 
' gewünschte Abfrage vorhanden?
if ( oAbfragen.hasByName( sQuery) ) Then
' Abfrage holen
oAbfrage = oAbfragen.getByName( sQuery )
' SQL-Ausdruck von Abfrage entnehmen
 sQuery = oAbfrage.Command
 ' Verbindung zur DB herstellen
 oVerbindung = oDatenquelle.getConnection( ,)
 ' Statement erzeugen
 oStatement = oVerbindung.createStatement
 ' Abfrage absetzen
 oResultSet = oStatement.executeQuery( oAbfrage.Command )
 
 ' durch alle Resultate iterieren
 while oResultSet.next()
' erste Spalte ausgeben
emplName = oResultSet.getString( 1 )
msgbox emplName
 Wend
 ' DB-Ressourcen wieder freigeben
 oResultSet.close()
 oStatement.close()
Else
 MsgBox "Keine Abfrage mit dem Name " & sQuery & " vorhanden"
 Exit Sub
End If
' Verbindung schliessen
 oVerbindung.close()

Does this help?
Cheers
Winfried

DateTime2 extension: insert date, time or timestamp, formatted to your needs
User avatar
dargosch
Posts: 48
Joined: Wed Jun 11, 2008 1:28 pm

Re: Macro to execute a simple SQL query in OObase

Post by dargosch »

Hi,

Yes, it does. Thank you! Although.. getting the database by name is not something I can do. This will be used within the context of a oobase form, and so the database would not have to me registered (I have nooo idea what the name will be even if it was).
So, how do I get a connection to the database to which the form belongs?

/Fredrik
--
"Life is like a trumpet - if you don't put anything into it, you don't get anything out of it."
OOo 3.0.X on Mac OSx Leopard
QuazzieEvil
Volunteer
Posts: 283
Joined: Tue Dec 04, 2007 6:38 pm
Location: Houston, TX

Re: Macro to execute a simple SQL query in OObase

Post by QuazzieEvil »

the form has a property called ActiveConneciton which refers to its connection

Code: Select all

Dim Conn As Object

Conn=Form.ActiveConnection
Post Reply