Skip to content
Framework

Database

db_execute_file()

Run every statement in a .sql file, in order.

Statements end at a ; outside quotes and comments (and PostgreSQL $$ quotes). SQLite trigger bodies stay whole. SQL Server files are split into batches at lines that hold only GO, as sqlcmd does. MySQL's client-side DELIMITER command is not supported.

Usage

db_execute_file(
    connection: Connection | str,
    path: str | Path,
    transaction: bool = True,
) -> int

Arguments

Name Type Default Description
connection required Connection | str

An open connection from db_connect(), or a connection name, which is opened for the file and closed afterward.

path required str | Path

The .sql file. A relative path is looked for from the project root, then from the current folder.

transaction bool True

Run the whole file in one transaction, so a failure leaves the database as it was. MySQL commits schema changes (CREATE, ALTER, DROP) at once whatever the client does. Pass False for statements that cannot run in a transaction, such as PostgreSQL's CREATE INDEX CONCURRENTLY or VACUUM; each statement then commits as it runs.

Value

The number of statements (or SQL Server batches) run.

Raises

  • FrameworkError: The file is missing, the connection is read-only, or a statement failed. The message names the statement's number and line.

Search guides and reference for R, Python, Stata, the CLI, and Framework Cloud.