DBConnection_v9 | R Documentation |
A robust database connection manager that handles connections to various database systems including Microsoft SQL Server and PostgreSQL. This class provides connection management, authentication, and automatic reconnection capabilities.
The DBConnection_v9 class encapsulates database connection logic and provides a consistent interface for connecting to different database systems. It supports both trusted connections and user/password authentication, handles connection failures gracefully, and provides automatic reconnection functionality.
Key features:
Support for multiple database systems (SQL Server, PostgreSQL)
Automatic connection management with retry logic
Secure credential handling
Connection status monitoring
Graceful error handling and recovery
config
Configuration details of the database.
connection
Database connection.
autoconnection
Database connection that automatically connects if possible.
new()
Create a new DBConnection_v9 object.
DBConnection_v9$new( driver = NULL, server = NULL, port = NULL, db = NULL, schema = NULL, user = NULL, password = NULL, trusted_connection = NULL, sslmode = NULL, role_create_table = NULL )
driver
Driver
server
Server
port
Port
db
DB
schema
Schema (e.g. "dbo")
user
User
password
Password
trusted_connection
NULL or "yes"
sslmode
NULL or "require"
role_create_table
NULL or the role to take when creating tables.
A new 'DBConnection_v9' object.
is_connected()
Is the DB schema connected?
DBConnection_v9$is_connected()
TRUE/FALSE
print()
Class-specific print function.
DBConnection_v9$print(...)
...
Not used.
connect()
Connect to the database
DBConnection_v9$connect(attempts = 2)
attempts
Number of attempts to be made to try to connect
disconnect()
Disconnect from the database
DBConnection_v9$disconnect()
clone()
The objects of this class are cloneable with this method.
DBConnection_v9$clone(deep = FALSE)
deep
Whether to make a deep clone.
## Not run:
# Create a SQL Server connection
db_config <- DBConnection_v9$new(
driver = "ODBC Driver 17 for SQL Server",
server = "localhost",
port = 1433,
db = "mydb",
user = "myuser",
password = "mypass"
)
# Connect to the database
db_config$connect()
# Check connection status
db_config$is_connected()
# Use the connection
tables <- DBI::dbListTables(db_config$connection)
# Disconnect when done
db_config$disconnect()
# PostgreSQL example
pg_config <- DBConnection_v9$new(
driver = "PostgreSQL",
server = "localhost",
port = 5432,
db = "mydb",
user = "myuser",
password = "mypass"
)
pg_config$connect()
# ... use connection ...
pg_config$disconnect()
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.