[Solved] Using MySQL Native Connector in OOBasic

dBase, Calc, CSV, MS ACCESS, MySQL, PostgrSQL, OTHER
Post Reply
PhilDaintree
Posts: 6
Joined: Wed Oct 28, 2009 10:39 am

[Solved] Using MySQL Native Connector in OOBasic

Post by PhilDaintree »

I posted this is in the beginners section - but maybe get more DB gurus looking in here?

I am trying to emulate some functionality I saw in Excel where a database is queried to return a value from the general ledger given a period reference and gl account code - this would enable scalc to be used as a GL report writer for webERP - an open source php web based ERP system. This would be a great reason to adopt open office for a lot of people.

I have read and searched widely on the use of the new connector. It seems relatively new so there is not much to see apart from descriptions of how to connect using the GUI. I have used Andrew Pitonyak's tutorial on OO Basic and accessing mysql - as modified for the URL identified from the OO wiki for the mysql connector. I have come up with the following code:


Code: Select all

Sub GetGLValue
   'Propose to send these parameters with the function call
   Dim AccountCode%
   Dim PeriodFrom%
   Dim PeriodTo%
   Dim BudgetOrActual%

   Dim sDatabaseName$
   Dim sUserName$
   Dim sUserPassword$
   Dim sDatabaseHost$

   sDatabaseName = "weberpdemo"
   sUserName = "theMySQLUserName"
   sUserPassword = "theMySQLUserPassword"
   sDatabaseHost = "localhost"
   AccountCode = 1380  'an account code in the demo
   PeriodTo = 22  'a period number to inquire on
   BudgetOrActual = 1
      
   Dim oStatement
   Dim oStaetment1
   Dim oResult
   Dim oResult1
   Dim oParams () As New com.sun.star.beans.PropertyValue
   Dim oManager
   Dim sURL
   Dim sSQL
   
   Dim sMessage
   
  oManager = CreateUnoService("com.sun.star.sdbc.DriverManager")
  
  AppendProperty(oParams(), "user", sUserName)
  AppendProperty(oParams(), "password", sUserPassword)
  
  sURL = "sdbc:mysqlc:://" & sDatabaseHost & ":3306/" & sDatabaseName
  
  oCon = oManager.getConnectionwithInfo(sURL, oParams())

  oStatement = oCon.CreateStatement()
  oResult1 = oStatement.executeQuery("USE " & DBQuoteName(sDatabaseName, oCon))

  sSQL = "SELECT " & DBQuoteName("bfwd", oCon) & "+" & _
          DBQuoteName("actual", oCon) & " AS TotValue FROM " &_
          DBQuoteName("chartdetails", oCon) & " WHERE " & _
          DBQuoteName("period", oCon) & "=" & PeriodTo & _
          " AND " & DBQuoteName("accountcode", oCon) & "=" & AccountCode & _
          " AND " & DBQuoteName("budgetoractual", oCon) & "=" & BudgetOrActual

  oStatement1 = oCon.CreateStatement()       
  oResult1 = oStatement.executeQuery(sSQL)

  Do While oResult1.next() 
    sMessage = sMessage & oResult1.getString(1) & CHR$(10)
  Loop
  MsgBox s, 0, "Value of Account: " & AccountCode & " for Period " & PeriodTo 
  oCon.close()

End Sub

Function DBQuoteName(sName As String, oCon) As String
  Dim sQuote As String
  sQuote = oCon.getMetaData().getIdentifierQuoteString()
  DBQuoteName = sQuote & sName & sQuote
End Function

Function CreateProperty(sName$, oValue) As com.sun.star.beans.PropertyValue
   Dim oProperty As New com.sun.star.beans.PropertyValue
   oProperty.Name = sName
   oProperty.Value = oValue
   CreateProperty() = oProperty
End Function

Sub AppendProperty(oProperties(), sName As String, ByVal oValue)
  AppendToArray(oProperties(), CreateProperty(sName, oValue))
End Sub

Sub AppendToArray(oData(), ByVal x)
  Dim iUB As Integer  'The upper bound of the array.
  Dim iLB As Integer  'The lower bound of the array.
  iUB = UBound(oData()) + 1
  iLB = LBound(oData())
  ReDim Preserve oData(iLB To iUB)
  oData(iUB) = x
End Sub
Sadly this code crashes.... anyone know why?

Also when using the URL "sdbc:mysqlc:://" & sDatabaseHost & ":3306/" & sDatabaseName

I am specifying the database name - weberpdemo - why then do I have to send SQL to the connection to get it to use the weberpdemo database? Is this a known bug with the connector.

I am using version 1.0.0 of the connector
I am using version 3.1.1 of OpenOffice OOO310m19 (Build: 9420)
I am using puppy linux 4.3.1 and mysql Ver 14.14 Distrib 5.1.35, for pc-linux-gnu (i686)

I may of course be reporting a bug but am assuming in the first instance that I have stuffed up somewhere... I would appreciate any help anyone can offer and in return I promise to write up a nice tutorial if I can get it going!!

What I really wanted to do was call the webERP API using XML-RPC this would side step any requirement for a data connector and would seem to be a more generic solution for getting data from web-services. However, I can't see any XML-RPC client code class available for OO Basic and coding such a thing is WAY beyond me!! If there is such a class available that would allow a value to be retrieved from an XML-RPC web-service and returned to an scalc function please could you point me to it.

Many thanks for reading!
Last edited by PhilDaintree on Thu Nov 12, 2009 8:51 am, edited 1 time in total.
Open Office 3.1 on Puppy Linux 4.3.1
FJCC
Moderator
Posts: 9625
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Using MySQL Native Connector in OOBasic

Post by FJCC »

I took a look at your code and I don't see any obvious problems, though I have little experience with database macros. Can you give some information about where the code is crashing? Just stepping through the code with the debugger might provide some very useful information.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
PhilDaintree
Posts: 6
Joined: Wed Oct 28, 2009 10:39 am

Re: Using MySQL Native Connector in OOBasic

Post by PhilDaintree »

Thanks for taking a look - sorry to have missed putting this important info in the original post.

When I use the code:

Code: Select all

oStatement = oCon.CreateStatement()
oResult1 = oStatement.executeQuery("USE " & DBQuoteName(sDatabaseName, oCon))
It crashes on the executeQuery .... no error message just the complete death of scalc and start recovery etc...

If I remove the above code and let it go straight to executing the real query then it errors saying... no database selected!

Perhaps there way of sending native mysql SQL that will not choke?

I have attempted to:

oCon.setSchema (sDatabaseName)

to set the database name but this method is not implemented.

I have tried hardcoding the sDatabaseName variable in

Code: Select all

 
  sURL = "sdbc:mysqlc:://" & sDatabaseHost & "/weberpdemo"
  oCon = oManager.getConnectionwithInfo(sURL, oParams())
Again the executeQuery errors with database not selected.

With an without a port specified.

Running out of ideas!
Open Office 3.1 on Puppy Linux 4.3.1
PhilDaintree
Posts: 6
Joined: Wed Oct 28, 2009 10:39 am

Re: Using MySQL Native Connector in OOBasic

Post by PhilDaintree »

Well I kept on searching and found:
founded myself in the metadata off the same connection but opened via the BaseContext
the url looks like: "sdbc:mysql:mysqlc:192.168.0.63:3306/pmgdbase"
thanks any howe

Fernand Vanrie wrote:

hallo all

i need to connect over a NATIVE Mysql connector

my code for a odbc connections is:
sURL = "sdbc:odbc:"myconnectionname"
'sdbc = protocol
'odbc = subprotocol
'myconnectionname = name off the wanted odbc connection


oManager = CreateUnoService("com.sun.star.sdbc.DriverManager")
oCon = oManager.getConnection(sURL)

wath sould the URL been for a native Mysql connector ???

thanks for any hint

Fernand
Thanks Fernand - looks like the documentation is incorrect in the wiki at

http://wiki.services.openoffice.org/wik ... ection_URL

I'd suggest this should be updated ASAP.

The URL as identified by Fernand above works for me!
Thanks Fernand
Open Office 3.1 on Puppy Linux 4.3.1
User avatar
r4zoli
Volunteer
Posts: 2882
Joined: Mon Nov 19, 2007 8:23 pm
Location: Budapest, Hungary

Re: Using MySQL Native Connector in OOBasic

Post by r4zoli »

I'd suggest this should be updated ASAP.
I suggest to you if you find anything wrong in wiki, please correct it on your own.
Use same name as in this forum, register to wiki, and you can correct any wiki page.
AOO 4.0 and LibO 4 on Win 8
Hungarian forum co-admin
PhilDaintree
Posts: 6
Joined: Wed Oct 28, 2009 10:39 am

Re: Using MySQL Native Connector in OOBasic

Post by PhilDaintree »

Ah ... super suggestion - have created an account and fixed it so that others don't fall in the same hole - thanks for alerting me to this possibility!
Open Office 3.1 on Puppy Linux 4.3.1
Post Reply