gpt4 book ai didi

python - SQLAlchemy 2.0 NotImplementedError : engine. 执行

转载 作者:行者123 更新时间:2023-12-04 12:32:34 24 4
gpt4 key购买 nike

我收到了 NotImplementedError: This method is not implemented for SQLAlchemy 2.0. , 尝试使用 delete 删除表时SQLAlchemy v1.4.15 中的方法。

from sqlalchemy import Column, Integer, MetaData, String, Table, create_engine, delete

engine = create_engine("sqlite+pysqlite:///:memory:", echo=True, future=True)
metadata = MetaData()

user = Table(
"users", metadata, Column("id", Integer, primary_key=True), Column("name", String)
)

metadata.create_all(engine)

engine.execute(user.delete()) # leading to Traceback
# NotImplementedError: This method is not implemented for SQLAlchemy 2.0.

最佳答案

engine.execute在 SQLAlchemy 1.4 中已弃用,并将在 SQLAlchemy 2.0 中删除。您需要改为调用连接的执行方法:

from sqlalchemy import Column, Integer, MetaData, String, Table, create_engine, delete

engine = create_engine("sqlite+pysqlite:///:memory:", echo=True, future=True)
metadata = MetaData()

user = Table(
"users", metadata, Column("id", Integer, primary_key=True), Column("name", String)
)

metadata.create_all(engine)

stmt = delete(user)
with engine.connect() as conn:
with conn.begin(): # Optional: start a transaction
conn.execute(stmt)

关于python - SQLAlchemy 2.0 NotImplementedError : engine. 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67719325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com