如何使用 postgresql 连接在 python 中生成报告?

How generate report in python with postgresql connection?

我尝试用 pyreportjasper 生成报告。用jaspersoft studio没问题,报告不错。但是当我尝试这段代码时,我遇到了这个错误:

init: Bootstrapping class not in BootstrapTypesSingleton.getInstance()[class=class org.python.core.PyStringMap]
Traceback (most recent call last):
  File "test.py", line 26, in <module>
    PyReportJasper.process_report()
  File "/usr/local/lib/python3.8/dist-packages/pyreportjasper/pyreportjasper.py", line 182, in process_report
    raise error
NameError: Error fill report: Erro fill internal: java.lang.NullPointerException

安装版本为:

java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)
java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)
python --version
Python 3.8.5

pyreportjasper 是 2.1.2。我的代码是:

from pyreportjasper import PyReportJasper

REPORTS_DIR = "/home/carto/reports/DW/"
input_file= REPORTS_DIR + "test.jrxml"
output_file="/home/carto/reports/DW/" + "S257E"
conn = {
        'driver': 'postgres',
        'username': 'postgres',
        'password': 'pass',
        'host': 'localhost',
        'database': 'db1',
        'schema': 'export',
        'port': '5432'
        }
PyReportJasper= PyReportJasper()
PyReportJasper.config (input_file,output_file,output_formats=["pdf"],parameters={'python_version': '1'},db_connection=conn,locale='fr-FR')

PyReportJasper.process_report()

错误出现在 PyReportJasper 的 db.py 中,但我不明白...但是 config.dbDriver 为空。为什么?

        elif dbtype == "postgres":      
            driver = config.dbDriver
            port = config.dbPort or 5434
            dbname = config.dbName
            connect_string = "jdbc:postgresql://{}:{}/{}".format(host, port, dbname)
        elif dbtype == "oracle":
            driver = config.dbDriver
            port = config.dbPort or 1521
            sid = config.dbSid
            connect_string = "jdbc:oracle:thin:@{}:{}:{}".format(host, port, sid)
        elif dbtype == "generic":
            driver = config.dbDriver
            connect_string = config.dbUrl


        self.Class.forName(driver)

要使用驱动程序,您需要添加 jdbc_driver 参数而不是:

conn = {
        'driver': 'postgres',
        'username': 'postgres',
        'password': 'pass',
        'host': 'localhost',
        'database': 'db1',
        'schema': 'export',
        'port': '5432'
        'jdbc_driver' : 'org.postgresql.Driver'
        }