Question: I'm using SQLAlchemy to connect to a SQLite database. I would like to execute some file, `script.sql`, which contains **multiple** SQL statements.

Previously, I was using the `sqlite3` library and running

```
with sqlite3.connect('my_database.db') as conn:
  with open('script.sql') as s:
    conn.executescript(s.read())

```

Moving to `SQLAlchemy`, I found [this question](https://stackoverflow.com/questions/2268050/execute-sql-from-file-in-sqlalchemy), from which I wrote new code using a `session` instance:

```
session.execute(text(s.read())

```

However, this returns an error, because my script contains multiple statements.

```
sqlite3.Warning: You can only execute one statement at a time.

```

What is the best way to execute a large script file with multiple statements using `SQLAlchemy`?

Answer: 
SQLAlchemy itself only calls the cursor's `execute` method, which does not support executing multiple statements. However it's possible to access the DB API connection directly, and call its `executescript` method:

```py
import sqlalchemy as sa

engine = sa.create_engine('sqlite:///so72341960.db')

# Create a table for our example.
tbl = sa.Table(
    'mytable',
    sa.MetaData(),
    sa.Column('id', sa.Integer, primary_key=True),
    sa.Column('name', sa.String),
)

# Ensure we start with a clean table.
tbl.drop(engine, checkfirst=True)
tbl.create(engine)

SQL = """
INSERT INTO mytable (name) VALUES ('Alice');
INSERT INTO mytable (name) VALUES ('Bob');
"""

with engine.begin() as conn:
    dbapi_conn = conn.connection
    dbapi_conn.executescript(SQL)

```
This is because sqlite3 only supports executing one statement at a time. If you want to execute multiple statements, they need to be separate commands. For example,

```py
statements = [] // put your statements in this array
for statement in statements:
  session.execute(text(statement))

```

This will iterate over the array of statements and execute each one in turn.