Write Python scripts

Many database tasks require more logic than simple SQLs. We do this using Python using JPython libraries. Python code can be started with the keyword python and ends with the first line with only a slash '/'.

Everything which works in Java works also in JPython. Here is an example:

connect db1

statement = sql.createStatement()
res = statement.executeQuery("select * from some_table")
meta_data = res.getMetaData()
col_count = meta_data.getColumnCount()
col_names = [str(meta_data.getColumnName(i + 1)) for i in range(col_count)]
print "Column count:", col_count, "names:", col_names
while res.next():
    values = [str(res.getObject(i + 1)) for i in range(col_count)]
    print dict(zip(col_names, values))
Python scripts receive two objects :

  • sql is the database connection.
  • connector is the DbConsole object for a defined connection