db

Provides methods for working with Data Bases through ODBC.

If you are using oxygen.conf.js, db module must be added.

modules: ['web', 'log', 'assert', 'date', 'db']

‚Äč

Before using this module, make sure to install unixodbc:

  • Windows - Install Windows SDK

  • OS X - brew install unixodbc

  • Linux - sudo apt-get install unixodbc unixodbc-dev or sudo dnf install unixODBC unixODBC-devel

executeNonQuery

Executes SQL statement.

Any results from the query are discarded.

Parameters:

Name

Type

Description

query

String

The query to execute.

db.executeNonQuery('UPDATE Vessel SET IMOCode = 9720471') // updated IMOCode

executeQuery

Executes SQL query and returns the result set.

Parameters:

Name

Type

Description

query

String

The query to execute.

Returns:

var vesselTable = db.executeQuery('SELECT * FROM data')
log.info(vesselTable) // prints the vessel table

Object - The result set.

getScalar

Executes SQL query and returns the first column of the first row in the result set.

Parameters:

Name

Type

Description

query

String

The query to execute.

Returns:

Object - The first column of the first row in the result set, or null if the result set is empty.

var vesselName = db.getScalar('SELECT VesselName FROM Vessel WHERE Voyage=15')
log.info(vesselName) // prints a single value, vessel name

setConnectionString

Sets DB connection string to be used by other methods.

This method doesn't actually open the connection as it's opened/closed automatically by query methods. Example connection strings:

  • Driver={MySQL ODBC 5.3 UNICODE Driver};Server=localhost;Database=myDatabase; User=myUsername;Password=myPassword;Option=3;

  • Driver={Oracle in instantclient_11_2};dbq=127.0.0.1:1521/XE;uid=myUsername; pwd=myPassword;

Parameters:

Name

Type

Description

connString

String

ODBC connection string.

db.setConnectionString('Driver={SQL Server};Server=DESKTOP-NAME\\SQLEXPRESS;Database=Vessels;Trusted_Connection=Yes');