Database Connector#

connect(connection_details=None, dbms=None, user=None, password=None, server=None, port=None, extra_settings=None, oracle_driver='thin', connection_string=None, path_to_driver=None)#

Connect to a OMOP CDM database

Connects to a database using the provided connection details, created by create_connection_details or from the arguments provided.

Wraps the R DatabaseConnector::connect function defined in DatabaseConnector/R/Connect.R.

Parameters:
  • connection_details (ListVector) – The connection details.

  • dbms (str | None, optional) – The DBMS type.

  • user (str | None, optional) – The database user name. Required if connection_string is not provided.

  • password (str | None, optional) – The database password. Required if connection_string is not provided.

  • server (str | None, optional) – The database server name. Required if connection_string is not provided.

  • port (int | None, optional) – The database server port. Required if connection_string is not provided.

  • extra_settings (str | None, optional) – Additional database connection settings.

  • oracle_driver (str, optional) – The Oracle driver to use. Defaults to thin.

  • connection_string (str | None, optional) – The database connection string. user, password, server, and port are ignored if this is provided.

  • path_to_driver (str | None, optional) – The path to the database driver jar file.

Returns:

The database connection.

Return type:

RS4

Examples

>>> connect(connection_details)
create_connection_details(dbms, user=None, password=None, server=None, port=None, extra_settings=None, oracle_driver='thin', connection_string=None, path_to_driver=None)#

Create Connection Details.

Creates a list containing all details needed to connect to a database. There are three ways to call this function:

  • create_connection_details(dbms, user, password, server, port,                                     extraSettings, oracleDriver,                                     pathToDriver)

  • create_connection_details(dbms, connectionString, pathToDriver)

  • create_connection_details(dbms, connectionString, user, password,                                     pathToDriver)

Wraps the R DatabaseConnector::createConnectionDetails function defined in DatabaseConnector/R/Connect.R.

Parameters:
  • dbms (str) – The DBMS type.

  • user (str | None, optional) – The database user name. Required if connection_string is not provided.

  • password (str | None, optional) – The database password. Required if connection_string is not provided.

  • server (str | None, optional) – The database server name. Required if connection_string is not provided.

  • port (int | None, optional) – The database server port. Required if connection_string is not provided.

  • extra_settings (str | None, optional) – Additional database connection settings.

  • oracle_driver (str, optional) – The Oracle driver to use. Defaults to thin.

  • connection_string (str | None, optional) – The database connection string. user, password, server, and port are ignored if this is provided.

  • path_to_driver (str | None, optional) – The path to the database driver jar file.

Returns:

The connection details.

Return type:

ListVector

Examples

>>> create_connection_details(
...     dbms="postgresql", user="user", password="password",
...     server="localhost/postgres", port=5432
... )
disconnect(connection)#

Disconnect from a database

Wraps the R DatabaseConnector::disconnect function defined in DatabaseConnector/R/Connect.R.

Parameters:

connection (RS4) – The database connection.

Return type:

None

Examples

>>> disconnect(connection)
execute_sql(connection, sql)#

Execute a SQL statement

Wraps the R DatabaseConnector::executeSql function defined in DatabaseConnector/R/Sql.R.

Parameters:
  • connection (RS4) – The database connection.

  • sql (str) – The SQL statement.

Return type:

None

Examples

>>> execute_sql(connection, sql)
>>> execute_sql(connection, "DROP TABLE IF EXISTS person")
>>> execute_sql(
...     conn, "CREATE TABLE x (k INT); CREATE TABLE y (k INT);"
... )
query_sql(connection, sql)#

Query a database

Wraps the R DatabaseConnector::querySql function defined in DatabaseConnector/R/Sql.R.

Parameters:
  • connection (RS4) – The database connection.

  • sql (str) – The SQL query.

Returns:

The query result.

Return type:

RS4

Examples

>>> query_sql(connection, sql)
>>> query_sql(connection, "SELECT COUNT(*) FROM person")