How do you integrate a .py file or a Python script into a .odb file?

Is Python really the recommended programming language for LibreOffice?
.
Disappointing observation and personal impression…
.

**Functionality      Basic   Python**
Integrated Editor:   Yes     No
Macros Creation:     Yes     No
Modification:        Yes     No
Debugging:           Yes     No
Base Integration:    Yes     No (completely incomplete)
Portability          ?       ?

.
This is objectively absurd for a language supposedly favored and recommended by the community…
.

No. It is one of many possible languages. Libre/Open/Staroffice has an API to be used also from Java, C++, Netbeans, JavaSript etc.
.
When StarOffice was created an Office Suite should have BASIC available for scripting, so that’s the one where you find most macros.
.
Today LibreOffice ships with BASIC, an integrated Python (on Linux mostly replaced by system python,) and a JavaScript Interpreter.
Use APSO to manage your Python macros.
.
You could also say C++ is the recommend language or Java. Use what suits your need. Your first question should be Interpreter or Compiler. The find out how to use it.
.
Imho for a lot of macros the language is not important, only the API is…

2 Likes

For integrating a python script to a Base file you will need a zip-app or
APSO. With APSO you could copy the file, which is stored in the python path of LO, to the database file.

The recommended programming language is Basic. There is an integrated editor so you could test all you have programmed directly and get feedback directly when code isn’t working well. It will show the “wrong” row and also a message what is wrong there.

If you know more about Python like I know it might be easy to write code without testing.

Thank you, I wanted to explore Python, but I found the integration with LibreOffice to be completely lacking. APSO seems like an interesting alternative. I also agree with you that a good understanding of APIs makes all the difference.

I usually use StarBasic, but I wanted to explore Python on both Linux and Windows, I realize I will need a folder that contains my Python scripts available on both platforms so that I can copy/paste them to the accessible location where LibreOffice expects to find them.

more exactly its THE tool for organizing, debugging and installing python–scripts both on windows and linux.
to explore the API you should use mri.oxt
as IDE you may choose any of simple editors like ( notepad, geany ect.) or full featured like ( VSCode, PyCharm …)

I myself enjoy working in a interactive Jupyter Notebook session, from which I can solve most of the tasks mentioned above.

I have not been able to pass parameters to queries for MariaDB’s connection.
As an exercise will try installing APSO in another (dummy) connection and construct a prepared statement using Python.
And try integrate it to the .odb of course.
But it’ll demand time…

Forms with subforms are the way to go.
Programmatically:

Sub Main
REM SELECT * FROM `dummy`.`Table1` AS `Table1` WHERE `Name` LIKE CONCAT( ?, '%' )
Const cQuery = "Param_StartsWith"
Const cColumn = "Name"
Const cParam = "C"
iType = com.sun.star.sdb.CommandType.QUERY
con = ThisDatabaseDocument.CurrentController.ActiveConnection
oCmd = con.prepareCommand(cQuery, iType)
REM param indices are 1-based!
oCmd.setString(1, cParam) 
oRS = oCmd.executeQuery()
'mri oRS
If oRS.first() AND oRS.isFirst() = True then
	nCol = oRS.findColumn(cColumn)
	Msgbox oRS.getString(nCol)
else
	Msgbox "No records"
endif
End Sub

Quick translation to Python:

def ParamQuery_MariaDB():
    from com.sun.star.sdb.CommandType import QUERY
    # SELECT * FROM `dummy`.`Table1` AS `Table1` WHERE `Name` LIKE CONCAT( ?, '%' )
    cQuery = "Param_StartsWith"
    cColumn = "Name"
    cParam = "C"
    iType = QUERY
    ThisDatabaseDocument = XSCRIPTCONTEXT.getDocument()
    con = ThisDatabaseDocument.CurrentController.ActiveConnection
    oCmd = con.prepareCommand(cQuery, iType)
    oCmd.setString(1, cParam)
    oRS = oCmd.executeQuery()
    if oRS.first() and oRS.isFirst():
        nCol = oRS.findColumn(cColumn)
        print(oRS.getString(nCol))
    else:
        print("No records")

Thanks for the code!
For this experiment no need of subform. Will try a simulacrum of old school post sent bank statements, with D/C accounting like layout.
SELECT from the view works fine hard coded, but not with parameters as it should be for practical purposes.
 
I have never tried APSO. Know how to run scripts via /[…]/Scripts/python/ of course.
I came to the conclusion that as there is no more the “native” LO’s driver, the generic MariaDB’s one does not parse Base :parameters :thinking:
But of course this is off-topic, so will stop here.
Thanks for the code again. I’ll try during end of year holidays.

Before you consider any programming language, you should definitively become familiar with the core functionality available without any macro code.
https://forum.openoffice.org/en/forum/viewtopic.php?t=42845

MariaDB_SDBC.odb (22.9 KB)

Back on topic, this is an arbitrary database document with some Python code embedded. Open the document with a zip browser, notice the Script/python/ directory the file therein and the entries in META-INF/manifest.xml. This has been done with APSO. It could be done manually as well.

I can only guess, that the only goal of the SDBC driver was the elimination of Java dependency, no matter what. ODBC and JDBC drivers can be used with named :parameters.

P.S. I tried to establish a most simple form/subform relation based on one parameter. I can’t accomplish the task with the SDBC driver for MySQL. Works perfectly well with many parameters of all types with ODBC and JDBC.

:grey_question:
JDBC here…
No matter I try be it in ISO format or American (my LO’s locale is set to US), query fails.
Please take notice that it is in parsed mode (of course!).
movimento_parameters
parameter_query_fails
 
Rem: the data in grid is from previous hard coded query.
 
Hard coded it runs :ok:
movimento_hard_coded

Direct connection runs with parameter here without any problem.
Also connection with JDBC will support parameters. Here I detected parameters should have length with more than 2 characters. Connection runs here with maraiadb-java-client-3.5.6.jar.
Might be you could choose something for named parameters in Edit → Database → Advanced Settings. This depends on the driver you use. (Bug 50747)

1 Like

Wow!
 
ReplaceNamedParameters
QueryRunsOK

Why is this on by default? Why/when should it be on anyway? And why is this option unavailable with the SDBC driver?

No, to me it was not. I checked it and then… parameter query worked!

I misunderstood the option. when checked, it accepts the :name and then sends ? to the engine, right?

When checked it accepts the parameters :+1:

What it sends I don’t know. But query runs :grimacing: