You can use a macro for this purpose, if I understand the question correctly, the answer is:
This code snippet will retrieve the String
in Cell “A1”, match it using the query string below, and output the result in Cell “A2”.
REM #load the object hierarchy
sheets = ThisComponent.Sheets
sheet1 = sheets.getByIndex(0)
REM #get the source cell
cellA1 = sheet1.getCellByPosition(0,0)
cellA1Text = cellA1.String
REM #connect to a database - mine happens to be number two on list
REM #name is dbName - can view value with MsgBox or manually input
dbContext = createUnoService("com.sun.star.sdb.DatabaseContext")
dbName = dbContext.registrationNames(2)
oDataSource = dbContext.getByName(dbName)
Conn = oDataSource.GetConnection("","")
REM #run the query using the connection
SQL = Conn.CreateStatement()
QueryStr = "SELECT `Latitude` FROM `citta` WHERE `City`= '" & cellA1Text & "'"
ResultSet = SQL.executeQuery(QueryStr)
ResultSet.next
outputValue = ResultSet.getDouble(1)
REM #close the database connection
Conn.close
oDataSource.dispose()
REM #output the result
outputCell = sheet1.getCellByPosition(1,0)
outputCell.String = outputValue
h/t to Dr. Bain for the connection methodology (page 10 of link). This is quick and ugly demo. The link shows some better modularization of code.
As for other details, as you can see we are working in the first Sheet
which we identified by Index number. Cell “A1” is in position 0,0
. I am loading the second registered database on my list. Your list will be different. You will need to change this number and make sure your LibreOffice database that connects to the MySQL database, is registered. I use backticks instead of double-quotes for field and table names in the SQL string in hopes of achieving higher compatibility. Output cell “A2” is in position 1.0
.
You will create this macro via Tools
→ Macros
→ Organize Macros
→ LibreOffice Basic
then go to the document click New
to make a new module, and paste that macro and customize. Press F5
to run, or go back to the macro dialog.