connect creates a connection to a database server .There are four ways to call this function:

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

  • connect(connectionDetails)

  • connect(dbms, connectionString, pathToDriver))

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

Arguments

connectionDetails

An object of class connectionDetails as created by the createConnectionDetails function.

dbms

The type of DBMS running on the server. Valid values are

  • "oracle" for Oracle

  • "postgresql" for PostgreSQL

  • "redshift" for Amazon Redshift

  • "sql server" for Microsoft SQL Server

  • "pdw" for Microsoft Parallel Data Warehouse (PDW)

  • "netezza" for IBM Netezza

  • "bigquery" for Google BigQuery

  • "sqlite" for SQLite

user

The user name used to access the server.

password

The password for that user.

server

The name of the server.

port

(optional) The port on the server to connect to.

schema

(DEPRECATED. optional) The name of the schema to connect to. DatabaseConnector attempts to set the schema using USE statement, but not all DBMS support USE statements. In DBMS that don't support USE statements this strategy will fail. Instead use a fully specified table names in your SQL statement, for example 'SELECT * FROM my_schema.my_table;'

extraSettings

(optional) Additional configuration settings specific to the database provider to configure things as security for SSL. These must follow the format for the JDBC connection for the RDBMS specified in dbms.

oracleDriver

Specify which Oracle drive you want to use. Choose between "thin" or "oci".

connectionString

The JDBC connection string. If specified, the server, port, extraSettings, and oracleDriver fields are ignored. If user and password are not specified, they are assumed to already be included in the connection string.

pathToDriver

Path to the JDBC driver JAR files. Currently only needed for BigQuery, Impala and Netezza. See jdbcDrivers for details on how to get the drivers.

Value

An object that extends DBIConnection in a database-specific manner. This object is used to direct commands to the database engine.

Details

This function creates a connection to a database.

DBMS parameter details

Depending on the DBMS, the function arguments have slightly different interpretations:

Oracle:

  • user. The user name used to access the server

  • password. The password for that user

  • server. This field contains the SID, or host and servicename, SID, or TNSName: '<sid>', '<host>/<sid>', '<host>/<service name>', or '<tnsname>'

  • port. Specifies the port on the server (default = 1521)

  • schema. DEPRECATED: This field contains the schema (i.e. 'user' in Oracle terms) containing the tables

  • extraSettings The configuration settings for the connection (i.e. SSL Settings such as "(PROTOCOL=tcps)")

  • oracleDriver The driver to be used. Choose between "thin" or "oci".

Microsoft SQL Server:

  • user. The user used to log in to the server. If the user is not specified, Windows Integrated Security will be used, which requires the SQL Server JDBC drivers to be installed (see details below).

  • password. The password used to log on to the server

  • server. This field contains the host name of the server

  • port. Not used for SQL Server

  • schema. DEPRECATED: The database containing the tables. If both database and schema are specified (e.g. 'my_database.dbo', then only the database part is used, the schema is ignored.

  • extraSettings The configuration settings for the connection (i.e. SSL Settings such as "encrypt=true; trustServerCertificate=false;")

Microsoft PDW:

  • user. The user used to log in to the server. If the user is not specified, Windows Integrated Security will be used, which requires the SQL Server JDBC drivers to be installed (see details below).

  • password. The password used to log on to the server

  • server. This field contains the host name of the server

  • port. Not used for SQL Server

  • schema. DEPRECATED: The database containing the tables

  • extraSettings The configuration settings for the connection (i.e. SSL Settings such as "encrypt=true; trustServerCertificate=false;")

PostgreSQL:

  • user. The user used to log in to the server

  • password. The password used to log on to the server

  • server. This field contains the host name of the server and the database holding the relevant schemas: <host>/<database>

  • port. Specifies the port on the server (default = 5432)

  • schema. DEPRECATED: The schema containing the tables.

  • extraSettings The configuration settings for the connection (i.e. SSL Settings such as "ssl=true")

Redshift:

  • user. The user used to log in to the server

  • password. The password used to log on to the server

  • server. This field contains the host name of the server and the database holding the relevant schemas: <host>/<database>

  • port. Specifies the port on the server (default = 5439)

  • schema. DEPRECATED: The schema containing the tables.

  • extraSettings The configuration settings for the connection (i.e. SSL Settings such as "ssl=true&sslfactory=com.amazon.redshift.ssl.NonValidatingFactory")

Netezza:

  • user. The user used to log in to the server

  • password. The password used to log on to the server

  • server. This field contains the host name of the server and the database holding the relevant schemas: <host>/<database>

  • port. Specifies the port on the server (default = 5480)

  • schema. DEPRECATED: The schema containing the tables.

  • extraSettings The configuration settings for the connection (i.e. SSL Settings such as "ssl=true")

  • pathToDriver The path to the folder containing the Netezza JDBC driver JAR file (nzjdbc.jar).

Impala:

  • user. The user name used to access the server

  • password. The password for that user

  • server. The host name of the server

  • port. Specifies the port on the server (default = 21050)

  • schema. DEPRECATED: The database containing the tables

  • extraSettings The configuration settings for the connection (i.e. SSL Settings such as "SSLKeyStorePwd=*****")

  • pathToDriver The path to the folder containing the Impala JDBC driver JAR files.

SQLite:

  • server. The path to the SQLIte file

To be able to use Windows authentication for SQL Server (and PDW), you have to install the JDBC driver. Download the .exe from Microsoft and run it, thereby extracting its contents to a folder. In the extracted folder you will find the file sqljdbc_4.0/enu/auth/x64/sqljdbc_auth.dll (64-bits) or sqljdbc_4.0/enu/auth/x86/sqljdbc_auth.dll (32-bits), which needs to be moved to location on the system path, for example to c:/windows/system32. If you not have write access to any folder in the system path, you can also specify the path to the folder containing the dll by setting the environmental variable PATH_TO_AUTH_DLL, so for example Sys.setenv("PATH_TO_AUTH_DLL" = "c:/temp") Note that the environmental variable needs to be set before calling connect for the first time.

Examples

if (FALSE) { conn <- connect(dbms = "postgresql", server = "localhost/postgres", user = "root", password = "xxx") dbGetQuery(conn, "SELECT COUNT(*) FROM person") disconnect(conn) conn <- connect(dbms = "sql server", server = "RNDUSRDHIT06.jnj.com") dbGetQuery(conn, "SELECT COUNT(*) FROM concept") disconnect(conn) conn <- connect(dbms = "oracle", server = "127.0.0.1/xe", user = "system", password = "xxx", pathToDriver = "c:/temp") dbGetQuery(conn, "SELECT COUNT(*) FROM test_table") disconnect(conn) conn <- connect(dbms = "postgresql", connectionString = "jdbc:postgresql://127.0.0.1:5432/cmd_database") dbGetQuery(conn, "SELECT COUNT(*) FROM person") disconnect(conn) }