sqlalchemy在sqlite中使用多个SQL语句执行脚本文件

发布于 2025-01-31 20:10:48 字数 713 浏览 3 评论 0原文

我正在使用SQLalchemy连接到SQLite数据库。我想执行一些文件,script.sql,其中包含多个 sql语句。

以前,我正在使用sqlite3库并运行

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

移动到sqlalchemy,我发现这个问题,我从中使用session实例编写了新代码:

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

但是,这返回错误,因为我的脚本包含多个语句。

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

使用sqlalchemy执行大型脚本文件的最佳方法是什么?

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, 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

避讳 2025-02-07 20:10:48

Sqlalchemy本身仅调用光标的execute方法,该方法不支持执行多个语句。但是,可以直接访问DB API连接,并调用其executescript方法:

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)

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:

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)
悲喜皆因你 2025-02-07 20:10:48

这是因为SQLite3仅支持一次执行一个语句。如果要执行多个语句,则需要单独命令。例如,

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

这将迭代一系列语句,然后依次执行每个语句。

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,

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文