LibreOffice BASIC : Connect to PostgreSQL - postgresql

I've created a PostgreSQL connection File using LibreOffice Base (6.1) and I can run SQL Queries in there just fine but I was wondering if it's possible to use this Base connection in a LibreOffice BASIC function.
I know you can use JDBC connections for MySQL
mysql://hostname:port/database_name
But I'm hoping there's a way to use the Base File seeing as it works so well
I've been trying to find documentation on this online but I am struggling to find anything that bridges the gap between BASIC and Base.

I've found the answer, the solution was to use createUnoService and that allows you to specify the name of the odb that was setup in Base.
oService = createUnoService("com.sun.star.sdb.DatabaseContext")
oBase = oService.getByName("basePostgreSQL")
oConn = oBase.getConnection("","")
oQuery = oConn.createStatement()
oSql = "select col from table"
oResult = oQuery.executeQuery(oSql)
while oResult.next()
msgBox oResult.getString(1)
wend
oConn.close()

Related

Is it possible to import data into MATLAB from a SAS database?

I'm working on an automation project and was wondering if it's possible to establish a connection between MATLAB and a SAS database?
Yes. For instance, you can use Matlab's sasread function to read from SAS
sasfile = 'C:\sasreaddemo.sas7bdat';
xlsfile = 'C:\SAS-Matlab Converter.xls';
[numeric,text,raw] = sasread(sasfile,xlsfile)
Example files are available:
https://www.mathworks.com/matlabcentral/fileexchange/15835-import-data-from-sas

There is a way to use lsp4e for calling language server methods directly?

I'm new to the lsp4e & lsp technologies and as far as I have seen the framework provides almost everything for working with eclipse. However there is a way to use this features at will? i.e I would like to use the LS to get all the functions on a file, I think this will be done with textDocument/documentSymbol but how can I get this using the lsp4e framework?
NOTE:
I checked for SymbolKind and seems it was not the one I was looking for however that input helped me finding a sample of DocumentSymbol
DocumentSymbolParams params = new DocumentSymbolParams(
new TextDocumentIdentifier(documentUri.toString()));
CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> symbols =
languageServer.getTextDocumentService().documentSymbol(params);
I checked for SymbolKind and seems it was not the one I was looking for. However that input helped me finding a sample of DocumentSymbol
DocumentSymbolParams params = new DocumentSymbolParams(
new TextDocumentIdentifier(documentUri.toString()));
CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> symbols =
languageServer.getTextDocumentService().documentSymbol(params);

Connecting MS Access to PostgreSQL Server

I'm having trouble with connecting to our postgres server. I was given the code used to connect ms access to our SQL Server. It shall be DSN-less, I have installed an ODBC-driver and now I'm stuck finding the correct connection string to write in access' vba code.
I know it has to be somewhat like this:
stConnect = "ODBC;Driver={PostgreSQL UNICODE};Data Provider=pgsqlODBC;hostaddr=[IP-adress];Port=61000;dbname=TE_TestFirst;user=admin;password=testtest;"
Or maybe like this:
stConnect = "ODBC;Driver={PostgreSQL UNICODE};Data Provider=pgsqlODBC;SERVER=[IP-adress];PORT=61000;DATABASE=TE_TestFirst;UID=admin;PWD=testtest;"
I have read that 64Bit-Pcs need to have been given the "Data Provider". Yet nothing works except the DSN version which we don't want. I've copied countless connection string from various sides, maybe I'm overseeing something?
I look forward to your replies and thank you in advance.
Puddingloeffel
I have found a way to create a User-DSN within a MS Access module, which then I use to connect to the server. It is no very pretty code, but it works. For those who look for a similar solution:
'Creating User DSN
strAttributes = "Description=I did this myself" & Chr(13) & "Server=[Servername/ID]" & Chr(13) & "Database=TE_TestFirst" & Chr(13) & "Username=admin" & Chr(13) & "Password=***" & Chr(13) & "Port=61000"
DBEngine.RegisterDatabase "DSNnew", "PostgreSQL Unicode", True, strAttributes
MsgBox "DSN has been created", vbInformation
Connection String
stConnect = "ODBC;DSN=DSNnew;Server=[IP-adress];Port=61000;Database=TE_TestFirst;Uid=admin;Pwd=****;"
As you can see the information is embedded twice, which I really don't like but for the moment it is okay. You can check your created DSN with the ODBC Administrator.
You can find the original code here
Also this code is the way we get the tables from the server to MS Access. Although the example is about SQL Server, it works fine with PostgreSQL as well! (As I see now this side also provides a solution about creating DSNs)
I haven't been able to solve the matter with a connection string that is DSN-less, but with a DSN file like #Erik van Asmuth and #krish KM said.
The DSN file is located in my Documents folder (ODBC Administrator saved it automatically in this folder. You can move it anywhere, but then you have to write the path instead of the file name)
I open it via VBA Code in MS Access with the following connection String:
stConnect = "ODBC;FILEDSN=PGUnicode32;Server=[IP-adress];Port=61000;Database=TE_TestFirst;Uid=admin;Pwd=****;"
Access got a connection and loads the table TE-TestFirst. Thank you guys again. I will now try to connect via connection string without the file
In the end we did the following:
Using connection string "PostgreSQL Unicode(x32)" written like shown above + using a different script to connect to
It now works fine and DSN-less :)
Something like this:
stConnect = "Driver={PostgreSQL Unicode(x64)};Servername=dbtest;Port=5432;UID=postgres;Database=postgres;Password=****";

See actual code of function in DB2 for IBM i

Hello guy I create a function in DB2 UDB for AS/400 version 07.01.0000 V7R1m0
I use a windows with dbvisualizer to connect the server.
My function is...
CREATE FUNCTION JVAOBJ.BNOWPAPOL(POL VARCHAR(10)) RETURNS DECIMAL(7,7) LANGUAGE SQL NOT
DETERMINISTIC READS SQL DATA
RETURN
(
SELECT
CASE
WHEN NUM IN (1,2)
THEN 0.3
ELSE 0.19698
END AS VALOR
FROM
LMDDTA.VERT240
WHERE
POLLIFE = POL )
It return 0.3 or 0.19698 depending of POL param
To do it I delete DROP FUNCTION JVAOBJ.BNOWPAPOL and run CREATE until work well.
My problem is I can not see the actual code of the function in dbvisualizer I cant see the function created
How I can see the actual code?
Note:
The server administrator has access to the console as400 (yes, that black screen with green letters or orange letters, which I have not much knowledge)
maybe, I can see it from here.
Note 2:
I use jt400 driver to connect.
Try IBM i Navigator for web. If it is configured on your machine, you can reach it through this URL: https://your.ibm.i:2005/ibm/console/login.do?action=secure
If it is not configured, then perhaps you can ask the admin to install the Windows client. It is part of Client Access for Windows and is called IBM i Navigator for Windows.
In either case, use the navigation tree to go to Databases > machine > Schemas > JVAOBJ > Functions. Right click your function > Definition and then choose Routine Body
EDIT Add SYSROUTINE
Another way to see the routine body is via the DB2 catalog table SYSROUTINE. select specific_schema, specific_name, routine_definition from sysroutine
I know IBM i Navigator its a great tool, but you need some amount of knowledge to master it.
The easiest way is to query the SQL system tables using dbvisualizer
SELECT * FROM qsys2/sysfuncs
WHERE (SPECIFIC_SCHEMA, SPECIFIC_NAME) = ('JVAOBJ', 'BNOWPAPOL')

How can I access a postgresql database from matlab with without matlabs database toolbox?

I tried already to use pgmex. Unfortunately it doesn't work with libpq5 (matlab immediately crashes).
To connect to postgres from matlab without the database toolbox do something similar to:
% Add jar file to classpath (ensure it is present in your current dir)
javaclasspath('postgresql-9.0-801.jdbc4.jar');
% Username and password you chose when installing postgres
props=java.util.Properties;
props.setProperty('user', '<your_postgres_username>');
props.setProperty('password', '<your_postgres_password>');
% Create the database connection (port 5432 is the default postgres chooses
% on installation)
driver=org.postgresql.Driver;
url = 'jdbc:postgresql://<yourhost>:<yourport>\<yourdb>';
conn=driver.connect(url, props);
% A test query
sql='select * from <table>'; % Gets all records
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
% Read the results into an array of result structs
count=0;
result=struct;
while rs.next()
count=count+1;
result(count).var1=char(rs.getString(2));
result(count).var2=char(rs.getString(3));
...
end
As a general solution, you can just use JDBC directly. Modern Matlabs all have a JVM embedded in them. Get the Postgresql JDBC driver JAR file on your Java CLASSPATH in Matlab and you can construct JDBC connection and statement objects. See "help javaclasspath".
There are a couple gotchas. Automatic registration of JDBC driver classes from JARs on the dynamic classpath in Matlab seems a little quirky, maybe because it uses a separate URL classloader and the core JDBC classes are in the system classloader. So you may need to explicitly construct instances of the JDBC driver class and pass them to the JDBC methods, instead of using the implicit driver construction that you see in all the JDBC tutorials. Also, there's performance overhead with each Java method call made from Matlab, which can become expensive if you're looping over a result set cursor in Matlab code. It's worthwhile to write a thin wrapper layer in Java that will wrap JDBC's iterative interface in a block-oriented Matlab-style interface, reading in the result sets and buffer them in arrays in Java, and passing the whole arrays back to Matlab.
You could use ODBC, too, but that requires writing MEX files linked against ODBC or working with ADO. More difficult and less portable.
EDIT: You can probably get the automatic driver registration stuff to work right if you get the JARs on your static Java classpath by using a custom classpath.txt.
I had a problem connecting to a pgsql database with matlab with SSL mode. Using the database toolbox it should be something like this:
conn = database('dbname','username','password','org.postgresql.Driver','jdbc:postgresql:databaseURL:dbname:ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory&')
but I had the error:
'FATAL: password authentication failed for user "username"'
So I use your script and get the same error.
I had to add the line
props.setProperty('ssl','true');
and the normal url, not with sslfactory ... as said in the matlab help.
So it's nice, but I cannot use the function of the database toolbox... well, not that much of a big deal!
Took me some time to find this out, so maybe it could be useful for other to know that if they have also problem connecting to a distant database in SSL mode on.
thanks !
Would MYSQL (additional link) work for you, at least as a starting point?
(DISCLAIMER: needs database toolbox)
Here is a full example to a ready setup postgresql server from a matlab script, adjust database parameters accordingly:
%Set preferences with setdbprefs.
setdbprefs('DataReturnFormat', 'cellarray');
setdbprefs('NullNumberRead', 'NaN');
setdbprefs('NullStringRead', 'null');
%Make connection to database.
%Using JDBC driver.
conn = database('mydb', 'USERNAME', 'YOURPASSWORD', 'Vendor',...
'POSTGRESQL', 'Server', 'SERVERIP', 'PortNumber', 5432);
%Read data from database, just an example on weather table in mydb database
curs = exec(conn, ['SELECT weather.city'...
' , weather.temperature'...
' FROM "mydb"."public".weather ']);
curs = fetch(curs);
close(curs);
%Assign data to output variable
untitled = curs.Data;
%Close database connection.
close(conn);
%Clear variables
clear curs conn
Your user needs LOGIN rights role and be able to access the tables (GRANT)