How to Run a SQL Script File in Python with sqlite3 executescript()
Setting up a database usually takes several statements: create the tables, add some starting rows. Keeping them in a .sql file is tidy, but execute() runs only one statement at a time. executescript() runs them all. This guide runs a small script file against SQLite, with real output from Python 3.15.2.
Syntax
connection.executescript(<SQL script>)
| Part | What it means |
|---|---|
<SQL script> | A string with one or more SQL statements, each ending in ; |
Python's sqlite3 documentation: "Create a new Cursor object and call executescript() on it with the given sql_script." And the cursor's executescript(): "Execute the SQL statements in sql_script."
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the SQL Script
Create books.sql next to the Python script. It creates a table and adds two books:
CREATE TABLE books (title TEXT, year INTEGER);
INSERT INTO books VALUES ('The Glass Orchard', 2019);
INSERT INTO books VALUES ('Rivers of Salt', 2023);
Three statements, each ending in ;.
Step 2: Run It from Python
import sqlite3
from pathlib import Path
script = Path("books.sql").read_text(encoding="utf-8")
connection = sqlite3.connect(":memory:")
connection.executescript(script)
print(connection.execute("SELECT title, year FROM books").fetchall())
connection.close()

Path("books.sql").read_text(encoding="utf-8")reads the whole file into one string.executescript(script)runs every statement in it, in order.- The
SELECTafterwards checks what the script built.
uv run python run_script.py
[('The Glass Orchard', 2019), ('Rivers of Salt', 2023)]

One call created the table and inserted both rows: the SELECT found everything the script wrote.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Passing the script to execute() | sqlite3.ProgrammingError: You can only execute one statement at a time. | Use executescript() |
Passing values to executescript(), like executescript("SELECT ?", ("x",)) | TypeError: Connection.executescript() takes exactly one argument (2 given) | Run statements that need values with execute() and ? placeholders |
| Running the script from another folder | read_text() fails: FileNotFoundError: [Errno 2] No such file or directory: 'books.sql' | Run it from its own folder, or build the path with Path(__file__).parent / "books.sql" |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
ProgrammingError: You can only execute one statement at a time. | Several statements went to execute() | Switch that call to executescript() |
FAQ
Does executescript() commit?
Only in one case. The documentation: with the default transaction handling, "and there is a pending transaction, an implicit COMMIT statement is executed first. No other implicit transaction control is performed; any transaction control must be added to sql_script." Put BEGIN; and COMMIT; in the script when it must all succeed or all fail.
Can the script hold comments?
Yes: it's ordinary SQL, so -- comments work.
Conclusion
executescript() ran the three statements of books.sql in one call, and the SELECT found the table and both books.
Additional Resources
- Connection.executescript — the shortcut on the connection.
- Cursor.executescript — what it runs and how it commits.
