Which Database driver do you use for connecting to postgreSQL from a Jython JSR223 script? Does anybody use psycopg2?
Info:
I want to query data from a postgreSQL database from Jython scripts and/or modules. I already programmed some with python and psycopg2 library.
As I do now know how to install/use psycopg2 in jython (has anybody managed to use it?), I took the java jdb driver from postgresql.org instead.
This works, but the interface is different, therefore I would have to change the programming somewhat.
jython script:
import os
import sys
from java.util import Properties
sys.path.append('/openhab/conf/automation/lib/java/postgresql-42.2.5.jar')
import org.postgresql.Driver as Driver
props = Properties()
props.put('user','uuuuu')
props.put('password','ppppp')
props.put('dbname','nnnnn')
conn = Driver().connect('jdbc:postgresql://172.19.0.2:5432/openhabian', props)
st = conn.createStatement();
rs = st.executeQuery ("SELECT speciesid, speciesname, mqtttopicname \
FROM public.species where mqtttopicname is not null and mqtttopicname != '' \
ORDER by species.order")
while (rs.next()):
do.log.info(" db query returned, species = " + rs.getString(2) + ", topic = " + rs.getString(3)
rs.close()
db.close()