如何在Python SQLAlchemy中从Oracle表中删除记录[重复]

wnavrhmk  于 2023-05-16  发布在  Oracle
关注(0)|答案(1)|浏览(204)

此问题已在此处有答案

SQLAlchemy 2.0 with engine.connect() … does not automatically commit(1个答案)
8天前关闭
传递delete语句不会从表中删除记录。代码成功执行,没有错误/警告,但记录没有被删除。
我尝试运行以下代码来使用sqlalchemy引擎(OracleDB)删除一行

sql = "DELETE FROM table WHERE id=1"

engine.connect().execute(text(sql))

日志:

cigdeys3

cigdeys31#

我相信你需要添加autocommit

from sqlalchemy.engine import create_engine

engine = create_engine("oracle+cx_oracle://user:password@hostname:1521/?servic_eanme=ServiceName", echo=True)
conn = engine.connect()
conn = conn.execution_options(autocommit=True)

query = "DELETE FROM temp_student WHERE id={id}"
conn.execute(query.format(id=2))

相关问题