db_connect: Function to create a database connection with a 'JSON'...

View source: R/db_connect.R

db_connectR Documentation

Function to create a database connection with a JSON configuration file.

Description

db_connect uses a JSON configuration file to create a database connection. This configuration file will often exist outside a code package so database credentials are not accidentally transmitted or shared.

Usage

db_connect(
  file,
  database,
  config = TRUE,
  foreign_keys = TRUE,
  sslmode = NULL,
  bigint = "integer64"
)

Arguments

file

JSON file or string containing database connection details. For SQLite databases, use the database's file path.

database

The name of the database within file to use to create a database connection to. If only one entry is in file, this argument is not needed and will be ignored if used.

config

A logical to skip the JSON file configuration and just attempt to connect to file directly. This is used for SQLite databases which require no configuration.

foreign_keys

A logical for SQLite databases where foreign keys should be enforced. Default is TRUE. For other database types, this will be ignored.

sslmode

For PostgreSQL databases, what sslmode should be used for the connection?

bigint

For 64-bit integers, how should these be handled? See dbConnect for details.

Details

If only one entry is in the JSON file, the database argument is not needed.

If db_connect is attempted to be used with a file which is not JSON, it will attempt to connect to an SQLite database, but it is recommended that the config argument is set to FALSE in this case.

MySQL, PostgreSQL, and SQLite connections are currently supported.

Value

Database connection.

Author(s)

Stuart K. Grange

Examples


## Not run: 

# Connect to an air quality database
db <- db_connect("connections.json", "air_quality")


# Use a json config file which looks similar to this:
string <- '
{
  "driver": "MySQL",
  "host": "172.31.4.159",
  "database_name": "database_seven",
  "user": "web",
  "password": "read_password"
}'

# Connect, no need for second argument when one connection is present in 
# configuration file
db_seven <- db_connect(string)


# A SQLite connection needs no configuration
con <- db_connect("../databases/air_quality_data.db", config = FALSE)


## End(Not run)


skgrange/databaser documentation built on Sept. 26, 2024, 8:30 p.m.