LibreOffice Calc执行PostgreSQL函数

时间:2017-03-16 13:08:01

标签: sql-server excel postgresql stored-procedures libreoffice

我目前正在使用Microsoft Excel在SQL Server数据库上执行存储过程,并且工作正常。

如果有人有兴趣,这里有非常好的指示 http://datapigtechnologies.com/blog/index.php/running-a-sql-stored-procedure-from-excel-with-dynamic-parameters/

我想知道是否可以使用LibreOffice Calc和PostgreSQL做这样的事情。

我知道LibreOffice支持PostgreSQL连接,因为你可以创建一个PostgreSQL odb文件,但我想知道是否可以像Excel一样执行存储的函数/函数

1 个答案:

答案 0 :(得分:1)

可以在LibreOffice Calc中执行类似的操作,但不是使用各种菜单设置数据库连接,而是使用宏代码完成所有操作。

以下使用this MySQL stored procedure为我工作:

Sub RunStoredProc
    Dim oParms(1) as new com.sun.star.beans.PropertyValue 
    oParms(0).Name = "user" 
    oParms(0).Value = "root" 
    oParms(1).Name = "password" 
    oParms(1).Value = "password" 
    oManager = CreateUnoService("com.sun.star.sdbc.DriverManager")
    sURL = "sdbc:mysql:jdbc:localhost:3306/world"
    oConnection = oManager.getConnectionWithInfo(sURL, oParms())
    sFormat = "Europe"
    oStmt = oConnection.prepareCall("CALL country_hos(?)")
    oStmt.setString(1, sFormat)
    oResult = oStmt.executeQuery()
    sResult = ""
    If Not IsNull(oResult) Then
      While oResult.Next()
        sResult = sResult & oResult.getString(1) & CHR(10)
      Wend
    End If
    MsgBox "Result: " & sFormat & " = " & CHR(10) & sResult
    oStmt.close()
End Sub

代码改编自https://forum.openoffice.org/en/forum/viewtopic.php?f=21&t=41149

要完成代码,请对其进行修改,将结果放入电子表格,而不是在消息框中显示。同时从下拉框中读取所选值,而不是硬编码sFormat的值。

注意:在线提供的一些信息建议使用中间.odb文件。这将涉及更多的菜单,而不是在宏中做所有事情。这适用于表和查询,但显然不适用于存储过程,除非可能使用HSQLDB here