# Connect to MotherDuck
con <- DBI::dbConnect(duckdb::duckdb(dbdir = tempfile()))
# List available databases
list_databases(con)
# Change to a specific database and schema
cd(con, database_name = "analytics_db", schema_name = "public")
# Disconnect
DBI::dbDisconnect(con)Change Active Database and Schema
Source: R/utils-md.R
Switches the active database and (optionally) schema for a valid DuckDB or MotherDuck connection. The function validates the target database and schema before executing the USE command and provides user feedback via CLI messages.
Usage
cd(.con, database_name, schema_name, silent = FALSE)Arguments
-
.con -
A valid
DBIconnection (DuckDB / MotherDuck). -
database_name -
A character string specifying the database to switch to. Must be one of the available databases returned by
list_all_databases(). -
schema_name -
(Optional) A character string specifying the schema to switch to within the given database. Must be one of the available schemas returned by
list_current_schemas(). -
silent -
to print message or not, default is FALSE
Value
Invisibly returns a message summarizing the new connection context. Side effects include printing CLI headers showing the current user and database context.
Details
The cd() function is analogous to a “change directory” command in a file system, but for database contexts. It updates the currently active database (and optionally schema) for the given connection. If the target database or schema does not exist, the function aborts with a descriptive CLI error message.
See Also
Other db-meta: launch_ui(), pwd(), summary()