Skip to main content

How to Run a SQL Script File in Python with sqlite3 executescript()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
connection.executescript(<SQL script>)
PartWhat 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:

books.sql
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​

run_script.py
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()

The run_script.py script in a macOS-style code window: it reads books.sql into a string with read_text, runs it with executescript on an in-memory database, then prints all rows of the books table.

  • 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 SELECT afterwards checks what the script built.
Terminal
uv run python run_script.py
Output
[('The Glass Orchard', 2019), ('Rivers of Salt', 2023)]

The output in a terminal: a list with the two books from the script, The Glass Orchard 2019 and Rivers of Salt 2023.

One call created the table and inserted both rows: the SELECT found everything the script wrote.

Common Mistakes​

MistakeWhat happensDo 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 folderread_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​

SymptomLikely causeFix
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​